Django models: mutual references between two classes and impossibility to use forward declaration in python

前端 未结 1 1024
暗喜
暗喜 2021-01-01 15:59

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

class User(models.Model):
    # ...
    loves = models.ManyToManyField(Article, relat         


        
相关标签:
1条回答
  • 2021-01-01 16:28

    You can find the solution in the docs:

    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):
        # ...
    
    0 讨论(0)
提交回复
热议问题