[英]How to serialize mutiple querysets in django rest framework?
I have two models that area related to each other.我有两个相互关联的模型。 One represents a project and the other one represents the field/area that the project bnelongs to.
一个代表一个项目,另一个代表该项目所属的领域/区域。
I have a view that looks like this:我有一个看起来像这样的视图:
class ProjetosArea(generics.ListAPIView):
lookup_field = 'id'
serializer_class = ProjetosPreviaSerializer
pagination_class = ProjectPagination
def get_queryset(self):
area_id = self.kwargs['id']
area = AreaConhecimento.objects.get(id=area_id)
projetos = Projeto.objects.filter(area_conhecimento=area_id)
queryset = [area, projetos]
return queryset
I want to write a serializer for this view that looks like this:我想为此视图编写一个序列化程序,如下所示:
{
"area": {---fields of the area---},
"projects": [---The projects that belongs to that area---]
}
How would write a serializer for this?如何为此编写序列化程序? The queryset is a list with the area and the projects belonging to that area
查询集是一个包含该区域和属于该区域的项目的列表
This get_queryset
doesn't look right.这个
get_queryset
看起来不正确。 The idea is to return a single queryset there to perform permissions check etc in there.这个想法是在那里返回一个查询集以在那里执行权限检查等。 You should not return two resources like that.
你不应该像这样返回两个资源。
Although if you need it - the code is just a tool.虽然如果您需要它 - 代码只是一个工具。 Do with it whatever you need.
随心所欲地使用它。
I'd suggest to keep get_queryset
as it is and overwrite list
method instead as that's what you really want to do.我建议保持
get_queryset
原样并覆盖list
方法,因为这是您真正想要做的。 Because you are returning two resources, it needs to know how to handle them.因为您要返回两个资源,所以它需要知道如何处理它们。
From example I deduct that area_conhecimento
is area pk
.从示例中,我
area_conhecimento
是 area pk
。 So something like that should work.所以这样的事情应该有效。
class ProjetosArea(generics.ListAPIView):
lookup_field = 'area_conhecimento'
serializer_class = ProjetosAreaSerializer
pagination_class = ProjectPagination
def list(self, request, *args, **kwargs):
qs = self.filter_queryset(self.get_queryset())
area = qs.first().area_conhecimento
# you will have to adjust it for pagination
serializer = self.get_serializer({
'projects': qs,
'area': area
})
return Response(serializer.data)
And just the serializers are as simple as it gets.只是序列化器就这么简单。
from rest_framework import serializers
class AreaConhecimentoSerializer(serializers.ModelSerializer):
class Meta:
model = AreaConhecimento
fields = '__all__'
class ProjetoSerializer(serializers.ModelSerializer):
class Meta:
model = Projeto
fields = '__all__'
class ProjetosAreaSerializer(serializers.Serializer):
area = AreaConhecimentoSerializer(read_only=True)
projects = ProjetoSerializer(many=True, read_only=True)
But your real problem is that you actually want to get area and its projects.但是您真正的问题是您实际上想要获得区域及其项目。 So just do it.
所以就去做吧。 You said it yourself
The projects that belongs to that area
.你自己
The projects that belongs to that area
。
class AreaRetrieveAPIView(RetrieveAPIView):
serializer_class = AreaSerializer
pagination_class = AreaPagination
class ProjetoSerializer(serializers.ModelSerializer):
class Meta:
model = Projeto
fields = '__all__'
class AreaSerializer(serializers.ModelSerializer):
projeto = ProjetoSerializer(many=True, read_only=True)
class Meta:
model = AreaConhecimento
fields = '__all__'
and it will return它会回来
{
"area": {
"projects": [---The projects that belongs to that area---],
---fields of the area---
},
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.