Django Rest框架 - 更新外键 [英] Django Rest Framework - Updating a foreign key

查看:721
本文介绍了Django Rest框架 - 更新外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Django Rest框架对此问题感到沮丧:

I am a bit frustrated with this problem using the Django Rest Framework:

我正在使用视图集 $ c>自定义序列化程序。此串行器的深度设置为1 。当我查询这个视图时,我得到正确的数据表示,例如:

I am using a viewset, with a custom serializer. This serializer has its depth set to 1. When i query this viewset I get the correct representation of data for example:

data = {
  id: 1,
  issue_name: 'This is a problem',
  status: {
    id: 3,
    name: 'todo'
  }
}

当需要更新状态时,问题 >。例如,如果我想为此问题选择另一个状态,例如:

The problem comes in when I need to update the status. For example if I want to select another status for this issue, for example:

status_new = {
   id: 4,
   name: 'done'
}

我发送以下PATCH回到服务器,这是输出:

I send the following PATCH back to the server, this is the output:

data = {
  id: 1,
  issue_name: 'This is a problem',
  status: {
    id: 4,
    name: 'done'
  }

}

但是,状态不会更新。事实上,它甚至不是validated_data字典的一部分。我已经看到嵌套关系是只读的。有人可以告诉我我需要用简单的方式做什么?

However, the status does not get updated. Infact, it is not even a part of the validated_data dictionary. I have read that nested relations are read-only. Could someone please tell me what I need to do this in a simple way?

真的有义务。

提前感谢

推荐答案

文档,您将需要编写自己的 create() update()方法,以支持可写嵌套数据。

As stated in the documentation, you will need to write your own create() and update() methods in your serializer to support writable nested data.

您还需要显式添加状态字段,而不是使用 depth 参数,否则我相信它不会自动添加到 validated_data

You will also need to explicitly add the status field instead of using the depth argument otherwise I believe it won't be automatically added to validated_data.

编辑:也许我有一点点细节:你想做的是在ModelIssueSerializer中覆盖更新。这将基本上拦截串行器级别上的PATCH / PUT请求。然后得到新的状态并将其分配给这样的实例:

Maybe I was a bit short on the details: what you want to do is override update in ModelIssueSerializer. This will basically intercept the PATCH/PUT requests on the serializer level. Then get the new status and assign it to the instance like this:

class StatusSerializer(serializers.ModelSerializer):
    class Meta:
        model = Status

class ModelIssueSerializer(serializers.ModelSerializer):
    status = StatusSerializer()
    # ...
    def update(self, instance, validated_data):
        status = validated_data.pop('status')
        instance.status_id = status.id
        # ... plus any other fields you may want to update
        return instance

我在评论中提到的原因可能需要添加一个 StatusSerializer 字段用于获取状态为 validated_data 。如果我记得正确,如果您只使用 depth ,那么嵌套对象可能不会在 update() / create()方法(尽管我可能会误会)。在任何情况下,添加 StatusSerializer 字段只是使用 depth = 1

The reason I mentioned in the comment that you might need to add a StatusSerializer field is for getting status into validated_data. If I remember correctly, if you only use depth then nested objects might not get serialized inside the update() / create() methods (although I might be mistaken on that). In any case, adding the StatusSerializer field is just the explicit form of using depth=1

这篇关于Django Rest框架 - 更新外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆