我有以下看法:
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 的限制。
那么如何在方法中设置某种标志呢?