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

        <i id='i4WW7'><tr id='i4WW7'><dt id='i4WW7'><q id='i4WW7'><span id='i4WW7'><b id='i4WW7'><form id='i4WW7'><ins id='i4WW7'></ins><ul id='i4WW7'></ul><sub id='i4WW7'></sub></form><legend id='i4WW7'></legend><bdo id='i4WW7'><pre id='i4WW7'><center id='i4WW7'></center></pre></bdo></b><th id='i4WW7'></th></span></q></dt></tr></i><div id='i4WW7'><tfoot id='i4WW7'></tfoot><dl id='i4WW7'><fieldset id='i4WW7'></fieldset></dl></div>
        • <bdo id='i4WW7'></bdo><ul id='i4WW7'></ul>
        <tfoot id='i4WW7'></tfoot>
        <legend id='i4WW7'><style id='i4WW7'><dir id='i4WW7'><q id='i4WW7'></q></dir></style></legend>
      2. Django模型:两个类之间的相互引用以及在python中无法使用前向声明

        时间:2023-08-30

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

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

          <tfoot id='VUhD9'></tfoot>

            <tbody id='VUhD9'></tbody>
          • <bdo id='VUhD9'></bdo><ul id='VUhD9'></ul>
          • <legend id='VUhD9'><style id='VUhD9'><dir id='VUhD9'><q id='VUhD9'></q></dir></style></legend>

                  本文介绍了Django模型:两个类之间的相互引用以及在python中无法使用前向声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我定义了两个模型,每个模型相互引用,如下所示:

                  I have defined two models where each one references the other, like so:

                  class User(models.Model):
                      # ...
                      loves = models.ManyToManyField(Article, related_name='loved_by')
                  
                  class Article(models.Model):
                      # ...
                      author = models.ForeignKey(User)
                  

                  你看,问题是两个类相互引用.无论这两个类以什么顺序实现,python总是引发NameError异常,抱怨一个类没有定义.

                  You see, the problem is both classes references each other. No matter in what order these two classes are implemented, python always raises NameError exception, complaining either one class is not defined.

                  推荐答案

                  您可以在 文档:

                  如果您需要在尚未定义的模型上创建关系,您可以使用模型的名称,而不是模型对象本身:

                  If you need to create a relationship on a model that has not yet been defined, you can use the name of the model, rather than the model object itself:

                  class Car(models.Model):
                      manufacturer = models.ForeignKey('Manufacturer')
                      # ...
                  
                  class Manufacturer(models.Model):
                      # ...
                  

                  这篇关于Django模型:两个类之间的相互引用以及在python中无法使用前向声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Django 管理员内联:select_related 下一篇:IntegrityError 在表“orders_order"上插入或更新违反外键约束“

                  相关文章

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

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

                      <tfoot id='wLx4D'></tfoot>
                    1. <small id='wLx4D'></small><noframes id='wLx4D'>