我是DRF的新手,剛剛開始構建API。我有兩個模型,一個用外鍵連接到父模型的子模型。這裏是模型我的簡化版本:Django Rest Framework序列化程序關係:如何獲取父級序列化程序中所有子對象的列表?
class Parent(models.Model):
name = models.CharField(max_length=50)
class Child(models.Model):
parent = models.ForeignKey(Parent)
child_name = models.CharField(max_length=80)
要創建序列化,我跟着DRF Serializer Relations,我已經創造了他們爲以下幾點:
class ChildSerializer(serializers.HyperlinkedModelSerializer):
parent_id = serializers.PrimaryKeyRelatedField(queryset=Parent.objects.all(),source='parent.id')
class Meta:
model = Child
fields = ('url','id','child_name','parent_id')
def create(self, validated_data):
subject = Child.objects.create(parent=validated_data['parent']['id'], child_name=validated_data['child_name'])
return child
class ParentSerializer(serializers.HyperlinkedModelSerializer):
children = ChildSerializer(many=True, read_only=True)
class Meta:
model = Course
fields = ('url','id','name','children')
我試圖讓父母序列化程序中所有孩子的列表。我想是能夠得到這樣的迴應:
{
'url': 'https://dummyapidomain.com/parents/1/',
'id': '1',
'name': 'Dummy Parent Name',
'cildren': [
{'id': 1, 'child_name': 'Dummy Children I'},
{'id': 2, 'child_name': 'Dummy Children II'},
{'id': 3, 'child_name': 'Dummy Children III'},
...
],
}
我沒想到這個工作,因爲在父模型,but it is the suggested way to do it in the documentation父母和孩子之間沒有任何聯繫,也沒有工作。
我收到以下錯誤信息:
Got AttributeError when attempting to get a value for field `children` on serializer `ParentSerializer`.
The serializer field might be named incorrectly and not match any attribute or key on the `Parent` instance.
Original exception text was: 'Parent' object has no attribute 'children'.
我認爲這是完全合理的,但我不明白我在這裏失蹤。
如何獲取父級序列化程序中所有子級的列表?
我相信反向關係的相關名稱默認爲'{field_name} _set',所以在這種情況下'children_set'。你應該可以在'ParentSerializer'中用'children_set'替換'children'' – jnishiyama