gpt4 book ai didi

Django Rest Framework 多个嵌套的可写序列化程序

转载 作者:行者123 更新时间:2023-12-04 19:29:57 27 4
gpt4 key购买 nike

我正在尝试使用 django rest 框架实现多个嵌套的可写序列化程序。我已阅读可用的文档 http://www.django-rest-framework.org/api-guide/serializers/#writable-nested-representations

我已经能够对一层嵌套执行此操作,但是当有多个嵌套时会出现问题。例如,我有这 3 个序列化程序:

class FarmerSerializer(serializers.ModelSerializer):
dob = serializers.DateField(write_only=True)
gender = serializers.CharField(write_only=True)

farms = FarmSerializer(required=False, many=True)

class Meta:
...

def create(self, validated_data):
# check if nested objects were provided, in particular farms
farms = validated_data.pop('farms', None)

farmer = Farmer(**validated_data)
farmer.added_by_id = validated_data.get('added_by_id')
farmer.save()

if farms is not None:
for farm in farms:
new_farm = Farm.objects.create(farmer=farmer, added_by=farmer.added_by, **farm)

# check for blocks -- fails
blocks = farm.pop('farm_blocks', None)
if blocks is not None:
for block in blocks:
FarmBlock.objects.create(farm=new_farm, added_by=farmer.added_by, **block)

return farmer

class FarmSerializer(serializers.ModelSerializer):
county = CountySerializer(read_only=True)
county_id = serializers.UUIDField(write_only=True)

farm_blocks = FarmBlockSerializer(required=False, many=True)

class FarmBlockSerializer(serializers.ModelSerializer):
...

我希望能够发布嵌套的 JSON 并保存所有 3 个级别,即农民、农场和农场块。当我保存前 2 个级别即农民和相关农场时,它目前有效。
如果有帮助,这里是我试图保存的 JSON 结构:
[
{
"name": "Ashley King",
"phone_number": "0765124764",
"gender": "F",
"dob": "1980-11-26",
"national_id": "29719008",
"farms": [
{
"name": "Big Farm 3",
"farm_size": "18",
"county_id": "5e208ba8-5f6c-4dac-a946-dada0c5250a2",
"constituency": "Nakuru",
"ward": "Town",
"town": "Nakuru",
"contact_name": "Winnie W.",
"contact_phone_number": "0724301432",
"farm_blocks": [
{"block_size": 4.56}
]
},
{
"name": "Big Farm 4",
"farm_size": "9.6",
"county_id": "5e208ba8-5f6c-4dac-a946-dada0c5250a2",
"constituency": "Nakuru",
"ward": "Town",
"town": "Nakuru",
"contact_name": "Winnie W.",
"contact_phone_number": "0724301432"
}
]
},
{
"name": "Dennis Wainaina",
"phone_number": "0764578389",
"gender": "M",
"dob": "1988-11-26",
"national_id": "27675654",
"farms": [
{
"name": "Big Farm 6",
"farm_size": "18",
"county_id": "5e208ba8-5f6c-4dac-a946-dada0c5250a2",
"constituency": "Nakuru",
"ward": "Town",
"town": "Nakuru",
"contact_name": "Winnie W.",
"contact_phone_number": "0724301432"
},
{
"name": "Big Farm 5",
"farm_size": "9.6",
"county_id": "5e208ba8-5f6c-4dac-a946-dada0c5250a2",
"constituency": "Nakuru",
"ward": "Town",
"town": "Nakuru",
"contact_name": "Winnie W.",
"contact_phone_number": "0724301432"
}
]
}
]

最佳答案

也许你可以编辑你的 create方法,有点像这样,

def create(self, validated_data):
farms = validated_data.pop('farms', None)
farmer = Farmer.objects.create(**validated_data)

if farms is not None:
for farm in farms:
#pop farm_blocks before creating new_farm
blocks = farm.pop('farm_blocks', None)
new_farm = Farm.objects.create(farmer=farmer, added_by=farmer.added_by, **farm)
#then create farm_bocks
if blocks is not None:
for block in blocks:
FarmBlock.objects.create(farm=new_farm, added_by=farmer.added_by, **block)

return farmer

关于Django Rest Framework 多个嵌套的可写序列化程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44774485/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com