Django Rest Framework嵌套的序列化器未显示相关数据 [英] Django Rest Framework nested serializer not showing related data
问题描述
我使用Django Rest框架进行了基本设置。我有两个模型和一个嵌套的序列化程序设置:
I have a basic setup using the Django Rest Framework. I have two models and a nested serializer setup:
# models.py
from django.db import models
class Plan(models.Model):
name = models.CharField(max_length='100')
def __unicode__(self):
return u'%s' % (self.name)
class Group(models.Model):
plan = models.ForeignKey('plan')
name = models.CharField(max_length='50')
weight = models.SmallIntegerField()
def __unicode__(self):
return u'%s - %s' % (self.name, self.plan.name)
# serializer.py
from plans.models import Plan, Group
from rest_framework import serializers
class GroupSerializer(serializers.ModelSerializer):
class Meta:
model = Group
fields = ('name', 'weight')
class PlanSerializer(serializers.ModelSerializer):
group = GroupSerializer(many=True, read_only=True)
class Meta:
model = Plan
fields = ('name', 'group')
# views.py
from rest_framework import viewsets
from plans.models import Plan
from plans.serializers import PlanSerializer
class PlanViewSet(viewsets.ModelViewSet):
queryset = Plan.objects.all()
serializer_class = PlanSerializer
当我在Django Shell中查看序列化程序关系时,它正确显示了该关系:
When I view the serializers relationships in Django's Shell it shows the relationship correctly:
PlanSerializer():
name = CharField(max_length='100')
group = GroupSerializer(many=True, read_only=True):
name = CharField(max_length='50')
weight = IntegerField()
我最终通过cURL回来的是:
What I end up getting back via cURL is:
[
{
name: Test Plan
}
]
我做什么预计会回来的是:
What I expect to get back is:
[
{
name: Test Plan,
group: [
{
name: Test Group,
weight: 1
}
]
}
]
没有嵌套数据通过。我对此处未正确设置的内容感到困惑。有人可以指出我正确的方向吗?
There is no nested data coming through. I'm at a lose for what I've not setup correctly here. Can anyone point me in the correct direction?
推荐答案
问题出在您的 queryset
: queryset = Plan.objects.all()
。此查询集中
中的所有项目都不具有 .group
属性,这就是为什么结果为空的原因。默认情况下,Django创建 plan
ForeignKey的反向关系,称为 group_set
(除非您不通过<$重命名) c $ c> related_name )(这意味着查询集中$ c $中的每个
的所有组。您可以使用此属性以获得正确的序列化。这意味着要更改:计划
项c>具有 group_set
属性,该属性是一个查询集,包含此计划
)
The problem comes from your queryset
: queryset = Plan.objects.all()
. None of the items in this queryset
has .group
attribute that's why your result is empty. By default Django creates a reverse relation of the plan
ForeignKey called group_set
(unless you don't rename it via related_name
) (this means that every plan
item in the queryset
have a group_set
attribute which is a queryset containing all the groups of this plan
). You can use this attribute in order to get a proper serialization. This means to change:
class PlanSerializer(serializers.ModelSerializer):
group_set = GroupSerializer(many=True, read_only=True)
class Meta:
model = Plan
fields = ('name', 'group_set')
如果您真的想坚持使用 group
(顺便说一句,这对于列表来说是一个非常糟糕的名字组)。您可以使用 prefetch_related
修改它,如下所示:
If you really want to stick with group
(btw this is a very bad name for a list of groups). You can hack it with prefetch_related
like so:
queryset = Plan.objects.prefetch_related('group_set', to_attr='group')
这样每 plan
项目将具有 group
属性-一个 queryset
包含以下所有组的属性此计划
。
this way every plan
item will have a group
attribute - a queryset
containing all the groups for this plan
.
这篇关于Django Rest Framework嵌套的序列化器未显示相关数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!