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中无法使用前向声明

        Django models: mutual references between two classes and impossibility to use forward declaration in python(Django模型:两个类之间的相互引用以及在python中无法使用前向声明)

          <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中无法使用前向声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                  How to extend Python class init(如何扩展 Python 类初始化)
                  What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                  What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                  Initialize list with same bool value(使用相同的布尔值初始化列表)
                  setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)
                  <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>
                    <tbody id='wLx4D'></tbody>
                    <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>
                          • <small id='wLx4D'></small><noframes id='wLx4D'>