Home > Mobile >  Django rest, hide output values in the response
Django rest, hide output values in the response

Time:07-21

I have a "published" table in models with the value true or false. I want to hide the ones with the false value in the response.enter image description here

#models
class Menu(models.Model):
    name = models.CharField(max_length=64)
    content = models.TextField(blank=True)
    time_create = models.DateTimeField(auto_now_add=True)
    time_update = models.DateTimeField(auto_now=True)
    published = models.BooleanField(default=True)
#views
class MenuAPIList(generics.ListCreateAPIView):
    """ обзор меню, доустпен всем зарег и нет """
    queryset = Menu.objects.all()
    serializer_class = MenuSerializer
    permission_classes = (IsAuthenticatedOrReadOnly, )
#serialisers
class MenuSerializer(serializers.ModelSerializer):
    user = serializers.HiddenField(default=serializers.CurrentUserDefault())
    class Meta:
        model = Menu
        fields = ("name", "content", "published", "user")

CodePudding user response:

Change the queryset on the View to filter the queryset used for the list response

class MenuAPIList(generics.ListCreateAPIView):
    queryset = Menu.objects.filter(published=True)
    ...
  • Related