我想要做的是通过经过身份验证的用户ID(模型上的外键)过滤资源上每个操作的查询集,如何在定义查询集时读取它?
class RunSessionViewSet(viewsets.ModelViewSet):
"""API endpoint for listing and creating sprints."""
queryset = RunningSession.objects.order_by('createdDate')
serializer_class = RunSessionSerializer
答案 0 :(得分:1)
您应该按照docs
中的说明覆盖get_queryset()
方法
def get_queryset(self):
user = self.request.user
return RunningSession.objects.filter(foreignkey_field=user).order_by('createdDate')