我有一个场景模型:
viewModel.ObitResults = obitResult
串行:
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')
instance = Scenario.objects.create(**validated_data)
for task_data in tasks:
print('task_data: ', task_data)
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, alidated_data)
for task_data in tasks:
task = Task.objects.get(pk=task_data.get('id'))
instance.tasks.add(task)
return instance
视图中的print语句打印:
elif request.method == 'POST':
print(request.data)
serializer = ScenarioSerializer(data=request.data)
if serializer.is_valid():
serializer.save(stakeholder=request.user)
return Response(serializer.data, status=status.HTTP_201_CREATED)
else:
return Response(
serializer.errors, status=status.HTTP_400_BAD_REQUEST)
序列化器中的print语句create()方法打印:
{u'tasks': [{u'what_can_go_wrong': u'', u'stakeholder': 2, u'why_perform_task': u'', u'how_often': u'', u'title': u'Seven', u'how_important_task': u'', u'people_involved': u'', u'special_training_required': None, u'why_important_task': None, u'project': 1, u'sequence_of_actions': u'', u'how_important_improvement': u'', u'role': u'', u'benefits_of_improvement': u'', u'effects_of_task': u'', u'tools_used': u'', u'special_vocabulary_used': u'', u'id': 7, u'any_improvements': u'', u'what_training_required': u''}, {u'what_can_go_wrong': u'', u'stakeholder': 2, u'why_perform_task': u'', u'how_often': u'DS', u'title': u'Eight', u'how_important_task': u'', u'people_involved': u'', u'special_training_required': None, u'why_important_task': None, u'project': 2, u'sequence_of_actions': u'', u'how_important_improvement': u'', u'role': u'', u'benefits_of_improvement': u'', u'effects_of_task': u'', u'tools_used': u'', u'special_vocabulary_used': u'', u'id': 8, u'any_improvements': u'', u'what_training_required': u''}]}
并抛出错误('task_data: ', OrderedDict([(u'title', u'Seven'), (u'how_often', u''), (u'how_important_task', u''), (u'role', u''), (u'why_perform_task', u''), (u'why_important_task', None), (u'sequence_of_actions', u''), (u'tools_used', u''), (u'special_training_required', None), (u'what_training_required', u''), (u'what_can_go_wrong', u''), (u'effects_of_task', u''), (u'special_vocabulary_used', u''), (u'people_involved', u''), (u'any_improvements', u''), (u'how_important_improvement', u''), (u'benefits_of_improvement', u''), (u'stakeholder', <User: reza123>), (u'project', <Project: One>)]))
为什么会这样?以某种方式,当它到达序列化器时,id会丢失?
更新:任务模型和TaskSerializer:
DoesNotExist: Task matching query does not exist.
虽然我收到了错误,但是创建了一个Scenario但没有任务。
堆栈跟踪:
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, )
...
class TaskSerializer(serializers.ModelSerializer):
class Meta:
model = Task
fields = '__all__'
答案 0 :(得分:2)
如下所示更改TaskSerializer
,
class TaskSerializer(serializers.ModelSerializer):
id = serializers.IntegerField()
class Meta:
model = Task
fields = '__all__'
并期望请求有效载荷为,
{
"tasks":[
{
"project" : 1,
"stakeholder":1,
"title":"title1",
"id":1111
},
{
"project" : 2,
"stakeholder":2,
"title":"title2",
"id":2222
}
],
}