DRF:简单的外键分配与嵌套的序列化程序? [英] DRF: Simple foreign key assignment with nested serializers?

查看:1225
本文介绍了DRF:简单的外键分配与嵌套的序列化程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Django REST框架,标准的ModelSerializer将允许通过以ID作为整数分配或更改ForeignKey模型关系。



最简单的是什么



注意,我只是在说现在的数据库对象,不是嵌套创建。



过去,我已经在这个过程中遇到了额外的id字段,并且使用自定义的创建更新方法,但是对于我来说,这是一个看似简单和经常的问题,我很好奇地知道最好的方法。

  class Child(models.Model):
name = CharField(max_length = 20)

class Parent(models.Model):
name = CharField(max_length = 20)
phone_number = models.ForeignKey(PhoneNumber)
child = models.ForeignKey(Child)

class ChildSerializer(M odelSerializer):
class Meta:
model = Child

class ParentSerializer(ModelSerializer):
#phone_number关系是自动的,将接受ID整数
children = ChildSerializer()#这个不会

class Meta:
model = Parent


解决方案

这里的最佳解决方案是使用两个不同的领域:一个用于阅读,另一个用于写作。 只读字段将是您的嵌套序列化程序(在这种情况下为 ChildSerializer ),它将允许您获得与您期望的相同的嵌套表示。大多数人把它定义为只是 child ,因为他们已经在这一点上写了他们的前端,改变它会导致问题。



只写字段将是 PrimaryKeyRelatedField ,这是通常用于根据主键分配对象。这不一定是只写的,特别是如果你想要在收到的内容和发送的内容之间进行对称,但是这听起来像是最适合你的。该字段应具有 a 设置为外键字段(在此示例中为 child ),因此它可以在创建和更新时正确分配。



< hr>

这已经在讨论组中提出了几次,我认为这仍然是最好的解决方案。感谢 Sven Maurer指出


With Django REST Framework, a standard ModelSerializer will allow ForeignKey model relationships to be assigned or changed by POSTing an ID as an Integer.

What's the simplest way to get this behavior out of a nested serializer?

Note, I am only talking about assigning existing database objects, not nested creation.

I have hacked away around this in the past with additional 'id' fields in the serializer and with custom create and update methods, but this is such a seemingly simple and frequent issue for me that I'm curious to know the best way.

class Child(models.Model):
    name = CharField(max_length=20)

class Parent(models.Model):
    name = CharField(max_length=20)
    phone_number = models.ForeignKey(PhoneNumber)
    child = models.ForeignKey(Child)

class ChildSerializer(ModelSerializer):
    class Meta:
        model = Child

class ParentSerializer(ModelSerializer):
    # phone_number relation is automatic and will accept ID integers
    children = ChildSerializer() # this one will not

    class Meta:
        model = Parent

解决方案

The best solution here is to use two different fields: one for reading and the other for writing. Without doing some heavy lifting, it is difficult to get what you are looking for in a single field.

The read-only field would be your nested serializer (ChildSerializer in this case) and it will allow you to get the same nested representation that you are expecting. Most people define this as just child, because they already have their front-end written by this point and changing it would cause problems.

The write-only field would be a PrimaryKeyRelatedField, which is what you would typically use for assigning objects based on their primary key. This does not have to be write-only, especially if you are trying to go for symmetry between what is received and what is sent, but it sounds like that might suit you best. This field should have a source set to the foreign key field (child in this example) so it assigns it properly on creation and updating.


This has been brought up on the discussion group a few times, and I think this is still the best solution. Thanks to Sven Maurer for pointing it out.

这篇关于DRF:简单的外键分配与嵌套的序列化程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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