我正在尝试根据当前用户过滤任务,如果没有人登录我想查看所有内容
这是我的观点类:
class TasksViewSet(viewsets.ModelViewSet):
def get_queryset(self):
queryset = Tasks.objects.filter(Q(assigner=self.request.user)| Q(assignee=self.request.user))
return queryset
queryset = Tasks.objects
serializer_class = TasksSerializer
permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
def perfrom_create(self, serializer):
serializer.save(self, assigner=self.request.user.pk)
这可以在用户登录时完成工作..如果没有人登录,我该怎么办才能查看?
答案 0 :(得分:1)
使用它:
def get_queryset(self):
user = self.request.user
queryset = Task.objects.all()
if not user.is_anonymous():
queryset = queryset.filter(Q(assigner=user)| Q(assignee=user))
return queryset
我建议你阅读一些文档:https://docs.djangoproject.com/en/1.11/ref/contrib/auth/#anonymoususer-object