gpt4 book ai didi

django - DRF Serializer 更新嵌套对象列表

转载 作者:行者123 更新时间:2023-12-05 01:42:36 25 4
gpt4 key购买 nike

我使用 Django-rest-framework 编写投票应用程序项目。它有两种模型:问题和嵌套答案。我想根据要求更新问题序列化程序中的答案,例如:

{
"question":"question",
"answers": [{"answer":"first updated answer"}, {"answer":"second update answer"}]
}

我怎样才能正确地做到这一点?我尝试这样做,但我删除了每个请求中的答案并创建了新的答案,因为每次都会更改它们的 ID。

序列化程序.py

class QuestionSerializer(serializers.ModelSerializer):
user = serializers.StringRelatedField()
answers = AnswerSerializer(many=True)

class Meta(object):
model = Question
fields = ('id', 'question', 'answers', 'total_votes', 'user')
read_only_fields = ('id', 'total_votes', )

def update(self, instance, validated_data):
instance.question = validated_data.get('question', instance.question)
instance.save()

question = Question.objects.get(id=instance.id)
Answer.objects.filter(question=question).delete()

answers_data = validated_data.pop('answers')
for answer_data in answers_data:
Answer.objects.create(question=question, **answer_data)

return instance

模型.py

class Question(models.Model):
question = models.CharField(verbose_name='Question', max_length=255)
total_votes = models.IntegerField(verbose_name='Total Votes', default=0)
user = models.ForeignKey(User, verbose_name='Owner', on_delete=models.CASCADE)

class Answer(models.Model):
question = models.ForeignKey(Question, verbose_name='Question', related_name='answers',
on_delete=models.CASCADE)
answer = models.CharField(verbose_name='Answer', blank=True, max_length=255)
votes_count = models.IntegerField(verbose_name='Count of Votes', default=0)

最佳答案

经过几个小时的文档浏览后才发现:

序列化程序.py

class AnswerSerializer(serializers.ModelSerializer):

# this is IMPORTANT, without this the 'id' field won't appear in validated data
id = serializers.IntegerField(required=False, write_only=False)

class Meta:
model = Answer

class QuestionSerializer(serializers.ModelSerializer):

user = serializers.StringRelatedField()
answers = AnswerSerializer(many=True)

def update(self, instance, validated_data):
answer_data = validated_data.pop("answers")

remove_items = { item.id: item for item in instance.answers.all() }

for item in answer_data:
item_id = item.get("id", None)

if item_id is None:
# new item so create this
instance.answers.create(**item)
elif remove_items.get(item_id, None) is not None:
# update this item
instance_item = remove_items.pop(item_id)
Answer.objects.filter(id=instance_item.id).update(**item)

for item in remove_items.values():
item.delete()

for field in validated_data:
setattr(instance, field, validated_data.get(field, getattr(instance, field)))
instance.save()

return instance

关于django - DRF Serializer 更新嵌套对象列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51448840/

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