1

我有以下看法:

class MessagesViewSet(ModelViewSet):
    """
    A simple ViewSet for viewing and editing the messages
    associated with the user.
    """
    authentication_classes = [TokenAuthentication, ]
    permission_classes = [IsAuthenticated]
    serializer_class = MessageSerializer
    filter_backends = [DjangoFilterBackend]
    filterset_fields = [MessageFields.MARK_READ]

    def get_user(self):
        user = self.request.user
        return user

    def get_queryset(self):
        return Message.objects.filter(sent_to=self.get_user())

    def perform_create(self, serializer):
        serializer.save(sender=self.get_user())

    @action(detail=True, )
    def unread_messages(self, request, pk):
        """
        Return all of the user's unread messages.
        """
        data = self.filter_queryset(self.get_queryset())
        serialized_data = MessageSerializer(data, many=True)
        return Response(serialized_data.data, status=HTTP_200_OK)

unread_messages我只想返回具有的对象mark_read = True(这是一个布尔字段)。现在它返回所有对象,因为方法中没有关于 True 或 False 的限制。

那么如何在方法中设置某种标志呢?

4

1 回答 1

0

你用mark_readis (likely)过滤它False

class MessagesViewSet(ModelViewSet):
    """
    A simple ViewSet for viewing and editing the messages
    associated with the user.
    """
    authentication_classes = [TokenAuthentication, ]
    permission_classes = [IsAuthenticated]
    serializer_class = MessageSerializer
    filter_backends = [DjangoFilterBackend]
    filterset_fields = [MessageFields.MARK_READ]

    def get_user(self):
        user = self.request.user
        return user

    def get_queryset(self):
        return Message.objects.filter(sent_to=self.get_user(), mark_read=False)

    def perform_create(self, serializer):
        serializer.save(sender=self.get_user())

    @action(detail=True, )
    def unread_messages(self, request, pk):
        """
        Return all of the user's unread messages.
        """
        data = self.filter_queryset(self.get_queryset())
        serialized_data = MessageSerializer(data, many=True)
        return Response(serialized_data.data, status=HTTP_200_OK)

或者您可以仅在unread_messageswith 中过滤这些:

class MessagesViewSet(ModelViewSet):
    """
    A simple ViewSet for viewing and editing the messages
    associated with the user.
    """
    authentication_classes = [TokenAuthentication, ]
    permission_classes = [IsAuthenticated]
    serializer_class = MessageSerializer
    filter_backends = [DjangoFilterBackend]
    filterset_fields = [MessageFields.MARK_READ]

    def get_user(self):
        user = self.request.user
        return user

    def get_queryset(self):
        return Message.objects.filter(sent_to=self.get_user())

    def perform_create(self, serializer):
        serializer.save(sender=self.get_user())

    @action(detail=True, )
    def unread_messages(self, request, pk):
        """
        Return all of the user's unread messages.
        """
        data = self.filter_queryset(self.get_queryset()).filter(mark_read=False)
        serialized_data = MessageSerializer(data, many=True)
        return Response(serialized_data.data, status=HTTP_200_OK)
于 2020-06-12T20:36:49.533 回答