以下是我的观点:
class SectorListAPI(generics.ListAPIView):
queryset = SectorModel.objects.all()
serializer_class = SectorSerializer
这是我的序列化程序:
^{pr2}$看,这里SectorSerializer'是父级'DepartmentSerializer'是子级,'OrganizationSerializer'是子级序列化器。现在在我看来,我可以很容易地过滤我的查询集来查找“SectorModel”。但是我怎样才能过滤出“GroupProfile”模型呢。在
您可能需要过滤queryset,以确保只返回与发出请求的当前经过身份验证的用户相关的结果。在
您可以根据
request.user.
的值进行过滤例如:
编辑
您可以将ListSerializer子类化并覆盖
to_representation
方法。在默认情况下,
to_representation
方法调用嵌套查询集上的data.all()
。因此,您实际上需要在调用方法之前生成data = data.filter(**your_filters)
。然后需要将子类ListSerializer添加为嵌套序列化程序的meta上的list_serializer_类。在1-子类
ListSerializer
,重写to_representation
,然后调用super2-在嵌套序列化程序上添加子类
ListSerializer
作为元list_serializer_class
。在与您相关的代码:
^{pr2}$相关问题 更多 >
编程相关推荐