在Django / REST中保存多对多模型?

时间:2022-10-04 16:05:43

I'm writing a REST API for my Django app, and can't get POST requests to work on one model. Here's the model in question:

我正在为我的Django应用程序编写REST API,并且无法获得POST请求以在一个模型上工作。这是有问题的模型:

class ProjectNode(models.Model):
    name = models.CharField(max_length=60)
    place = models.CharField(max_length=150)
    time_spent = models.BigIntegerField()
    parent_project = models.ForeignKey(Project, related_name='tasks')
    workers = models.ManyToManyField(User, related_name='tasks_can_do')

    def __str__(self):
        return self.name

The User model just holds a name field at the moment. Here's my serializer for ProjectNode:

用户模型此刻只保留一个名称字段。这是ProjectNode的序列化程序:

class ProjectNodeSerializer(serializers.ModelSerializer):
    class Meta:
        model = ProjectNode
        fields = ('id', 'name', 'place', 'time_spent', 'workers',)

And here's the API view (from views.py):

这是API视图(来自views.py):

class WebProjectNodeListView(generics.ListCreateAPIView):
    queryset = ProjectNode.objects.all()
    serializer_class = ProjectNodeSerializer

    def pre_save(self, obj):
        obj.parent_project = Project.objects.get(pk=self.request.DATA['parent_project'])
        for worker_pk in self.request.DATA['workers']:
            obj.workers.add(User.objects.get(pk=worker_pk))
        obj.final_worker = User.objects.get(pk=self.request.DATA['final_workers'])

I tried a simpler version yesterday at first, which only had the Project ForeignKey relationship, and it seemed to work, so I thought that using add would work too, but I get an error when testing out the API with httpie (I already added some users and projects, and am sure I get their id's correctly). Here's the request:

我昨天尝试了一个更简单的版本,它只有Project ForeignKey关系,它似乎有效,所以我认为使用add也会起作用,但是在使用httpie测试API时出错了(我已经添加了一些用户和项目,我相信我的ID正确)。这是请求:

http POST :8000/api/tasks/ name="newtask" place="home" time_spent:=50 parent_project:=1 workers:=[1]

And I get this error:

我收到这个错误:

"<ProjectNode: newtask>" needs to have a value for field "projectnode" before this many-to-many relationship can be used.

And the traceback also points to this line of code:

回溯也指向这行代码:

obj.workers.add(User.objects.get(id=worker_pk))

Now, I get the feeling that this is because I'm trying to update the relationship on the User object before a ProjectNode object is created in the database, but I'm not sure how to resolve this?

现在,我觉得这是因为我在数据库中创建ProjectNode对象之前尝试更新User对象上的关系,但我不知道如何解决这个问题?

1 个解决方案

#1


1  

DRF doesn't works create models which are nested serializers objects or Many to Many fields. So is necessary to override Serializer create method and create/get M2M models before create ProjectNode. Try to override create(self, validated_data) in your serializer and work with your data inside this method..

DRF不起作用创建嵌套序列化程序对象或多对多字段的模型。因此,在创建ProjectNode之前,必须覆盖Serializer create方法并创建/获取M2M模型。尝试覆盖序列化程序中的create(self,validated_data)并使用此方法中的数据。

Example:

My model Project has M2M relation with ProjectImages. In ProjectSerializer I override create method like this.

我的模型项目与ProjectImages有M2M关系。在ProjectSerializer中,我重写了这样的create方法。

 def create(self, validated_data):
        try:
            # Remove nested and M2m relationships from validated_data
            images = validated_data.pop('projectimage_set') if 'projectimage_set' in validated_data else []

            # Create project model
            instance = Project(**validated_data)
            if status:
                instance.set_status(status)

            project = instance.save()

            # Create relations
            for image in images:
                ProjectImage.objects.create(project=project, **image)

        except exceptions.ValidationError as e:
            errors_messages = e.error_dict if hasattr(e, 'error_dict') else e.error_list
            raise serializers.ValidationError(errors_messages)

        return project

Hope this help!

希望这有帮助!

#1


1  

DRF doesn't works create models which are nested serializers objects or Many to Many fields. So is necessary to override Serializer create method and create/get M2M models before create ProjectNode. Try to override create(self, validated_data) in your serializer and work with your data inside this method..

DRF不起作用创建嵌套序列化程序对象或多对多字段的模型。因此,在创建ProjectNode之前,必须覆盖Serializer create方法并创建/获取M2M模型。尝试覆盖序列化程序中的create(self,validated_data)并使用此方法中的数据。

Example:

My model Project has M2M relation with ProjectImages. In ProjectSerializer I override create method like this.

我的模型项目与ProjectImages有M2M关系。在ProjectSerializer中,我重写了这样的create方法。

 def create(self, validated_data):
        try:
            # Remove nested and M2m relationships from validated_data
            images = validated_data.pop('projectimage_set') if 'projectimage_set' in validated_data else []

            # Create project model
            instance = Project(**validated_data)
            if status:
                instance.set_status(status)

            project = instance.save()

            # Create relations
            for image in images:
                ProjectImage.objects.create(project=project, **image)

        except exceptions.ValidationError as e:
            errors_messages = e.error_dict if hasattr(e, 'error_dict') else e.error_list
            raise serializers.ValidationError(errors_messages)

        return project

Hope this help!

希望这有帮助!