gpt4 book ai didi

Django rest m2m 嵌套序列化器创建/更新嵌套对象

转载 作者:行者123 更新时间:2023-12-04 13:22:04 25 4
gpt4 key购买 nike

我有一个模型 场景 :

class Scenario(models.Model):
stakeholder = models.ForeignKey(User, related_name='scenarios', blank=True,)
tasks = models.ManyToManyField(Task, blank=True)

任务 对象已经创建,从我的前端,我通过向其中添加一些现有任务来创建一个场景。

序列化器:
class ScenarioSerializer(serializers.ModelSerializer):
tasks = TaskSerializer(many=True, required=False)
class Meta:
model = Scenario
fields = '__all__'

def create(self, validated_data):
tasks = validated_data.pop('tasks')
scenario = Scenario.objects.create(**validated_data)
for task in tasks:
Task.objects.update(scenario=scenario, **task)
return scenario

如果没有 create() 方法,它会抛出错误:
AssertionError: The `.create()` method does not support writable nested fields by default.
Write an explicit `.create()` method for serializer `scenarios.serializers.ScenarioSerializer`, or set `read_only=True` on nested serializer fields.

当我添加这个方法时,错误消失了,但它抛出了:
FieldError: Cannot update model field <ManyToManyRel: scenarios.scenario> (only non-relations and foreign keys permitted)

我究竟做错了什么?我不想创建新任务,只是创建一个新场景并将现有任务附加到它。

更新 :任务模型:
class Task(models.Model):
stakeholder = models.ForeignKey(User, related_name='tasks', blank=True, )
project = models.ForeignKey(Project, related_name='project_tasks' )
title = models.CharField(max_length=50, blank=True, null = True, )
OFTEN_CHOICES = (
('DS', 'Daily several times'),
('DO', 'Daily once'),
('WO', 'Weekly once'),
('MO', 'Monthly once'),
('YO', 'Yearly once'),
)
how_often = models.CharField(blank=True, null = True, choices=OFTEN_CHOICES, max_length=2, )
IMPORTANCE_CHOICES = (
('EI', 'Extremely important'),
('RI', 'Rather important'),
('LI', 'Less important'),
)
how_important_task = models.CharField(blank=True, null = True, choices=IMPORTANCE_CHOICES, max_length=2, )
role = models.CharField(max_length=20, blank=True, null = True, )
why_perform_task = models.CharField(max_length=150, blank=True, null = True, )
why_important_task = models.CharField(max_length=150, blank=True, null = True, )
sequence_of_actions = models.CharField(max_length=500, blank=True, null = True, )
tools_used = models.CharField(max_length=150, blank=True, null = True, )
special_training_required = models.NullBooleanField(blank=True, null = True, default=False, )
what_training_required = models.CharField(max_length=150, blank=True, null = True, )
what_can_go_wrong = models.CharField(max_length=150, blank=True, null = True, )
effects_of_task = models.CharField(max_length=150, blank=True, null = True, )
special_vocabulary_used = models.CharField(max_length=150, blank=True, null = True, )
people_involved = models.CharField(max_length=150, blank=True, null = True, )
any_improvements = models.CharField(max_length=150, blank=True, null = True, )
how_important_improvement = models.CharField(blank=True, null = True, choices=IMPORTANCE_CHOICES, max_length=2, )
benefits_of_improvement = models.CharField(max_length=150, blank=True, null = True, )

示例请求数据:
{"tasks":[{"id":7,"title":"Seven","how_often":"","how_important_task":"","role":"","why_perform_task":"","why_important_task":null,"sequence_of_actions":"","tools_used":"","special_training_required":null,"what_training_required":"","what_can_go_wrong":"","effects_of_task":"","special_vocabulary_used":"","people_involved":"","any_improvements":"","how_important_improvement":"","benefits_of_improvement":"","stakeholder":2,"project":1}]}

最佳答案

您完成了第一部分,现在需要添加方法 update这里docs :

例如

class ScenarioSerializer(serializers.ModelSerializer):
tasks = TaskSerializer(many=True, required=False)
class Meta:
model = Scenario
fields = '__all__'

def create(self, validated_data):
tasks = validated_data.pop('tasks', [])
instance = Scenario.objects.create(**validated_data)
for task_data in tasks:
task = Task.objects.get(pk=task_data.get('id'))
instance.tasks.add(task)
return instance

def update(self, instance, validated_data):
tasks = validated_data.pop('tasks', [])
instance = super().update(instance, validated_data)
for task_data in tasks:
task = Task.objects.get(pk=task_data.get('id'))
instance.tasks.add(task)
return instance

关于Django rest m2m 嵌套序列化器创建/更新嵌套对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50048851/

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