<tfoot id='fT3yf'></tfoot>

        <i id='fT3yf'><tr id='fT3yf'><dt id='fT3yf'><q id='fT3yf'><span id='fT3yf'><b id='fT3yf'><form id='fT3yf'><ins id='fT3yf'></ins><ul id='fT3yf'></ul><sub id='fT3yf'></sub></form><legend id='fT3yf'></legend><bdo id='fT3yf'><pre id='fT3yf'><center id='fT3yf'></center></pre></bdo></b><th id='fT3yf'></th></span></q></dt></tr></i><div id='fT3yf'><tfoot id='fT3yf'></tfoot><dl id='fT3yf'><fieldset id='fT3yf'></fieldset></dl></div>
          <bdo id='fT3yf'></bdo><ul id='fT3yf'></ul>

        <small id='fT3yf'></small><noframes id='fT3yf'>

        <legend id='fT3yf'><style id='fT3yf'><dir id='fT3yf'><q id='fT3yf'></q></dir></style></legend>

      1. DRF:使用嵌套序列化程序的简单外键分配?

        DRF: Simple foreign key assignment with nested serializers?(DRF:使用嵌套序列化程序的简单外键分配?)
          <tbody id='om3Pd'></tbody>
      2. <tfoot id='om3Pd'></tfoot>

            <bdo id='om3Pd'></bdo><ul id='om3Pd'></ul>

          • <i id='om3Pd'><tr id='om3Pd'><dt id='om3Pd'><q id='om3Pd'><span id='om3Pd'><b id='om3Pd'><form id='om3Pd'><ins id='om3Pd'></ins><ul id='om3Pd'></ul><sub id='om3Pd'></sub></form><legend id='om3Pd'></legend><bdo id='om3Pd'><pre id='om3Pd'><center id='om3Pd'></center></pre></bdo></b><th id='om3Pd'></th></span></q></dt></tr></i><div id='om3Pd'><tfoot id='om3Pd'></tfoot><dl id='om3Pd'><fieldset id='om3Pd'></fieldset></dl></div>
              <legend id='om3Pd'><style id='om3Pd'><dir id='om3Pd'><q id='om3Pd'></q></dir></style></legend>

              1. <small id='om3Pd'></small><noframes id='om3Pd'>

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

                  问题描述

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

                  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.

                  我过去在序列化程序中使用额外的id"字段以及自定义 createupdate 方法解决了这个问题,但这看起来很简单我很想知道最好的方法.

                  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.

                  只读字段将是您的嵌套序列化程序(在本例中为 ChildSerializer),它将允许您获得与您期望的相同的嵌套表示.大多数人将其定义为child,因为他们的前端已经写好了,改变它会导致问题.

                  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.

                  只写字段将是 PrimaryKeyRelatedField,这是您通常用于根据主键分配对象的方法.这不一定是只写的,特别是如果您试图在接收和发送的内容之间实现对称,但听起来这可能最适合您.该字段应将 a source 设置为外部关键字段(本例中为 child),以便在创建和更新时正确分配它.

                  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.

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

                  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:使用嵌套序列化程序的简单外键分配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Split a Pandas column of lists into multiple columns(将 Pandas 的列表列拆分为多列)
                  How does the @property decorator work in Python?(@property 装饰器在 Python 中是如何工作的?)
                  What is the difference between old style and new style classes in Python?(Python中的旧样式类和新样式类有什么区别?)
                  How to break out of multiple loops?(如何打破多个循环?)
                  How to put the legend out of the plot(如何将传说从情节中剔除)
                  Why is the output of my function printing out quot;Nonequot;?(为什么我的函数输出打印出“无?)
                1. <tfoot id='Du4kl'></tfoot>
                    <bdo id='Du4kl'></bdo><ul id='Du4kl'></ul>
                    <i id='Du4kl'><tr id='Du4kl'><dt id='Du4kl'><q id='Du4kl'><span id='Du4kl'><b id='Du4kl'><form id='Du4kl'><ins id='Du4kl'></ins><ul id='Du4kl'></ul><sub id='Du4kl'></sub></form><legend id='Du4kl'></legend><bdo id='Du4kl'><pre id='Du4kl'><center id='Du4kl'></center></pre></bdo></b><th id='Du4kl'></th></span></q></dt></tr></i><div id='Du4kl'><tfoot id='Du4kl'></tfoot><dl id='Du4kl'><fieldset id='Du4kl'></fieldset></dl></div>
                      <tbody id='Du4kl'></tbody>

                  • <small id='Du4kl'></small><noframes id='Du4kl'>

                      <legend id='Du4kl'><style id='Du4kl'><dir id='Du4kl'><q id='Du4kl'></q></dir></style></legend>