Django Model Definition Race

前端 未结 2 1770
梦如初夏
梦如初夏 2021-01-27 14:15

How do you deal with the case where you have a model (A) that relies on model (B) being defined, but model (B) also relies on model(A) being defined.

For instance, I hav

相关标签:
2条回答
  • 2021-01-27 14:39
    class event(models.Model):
      competition_start = models.DateField() 
      competitors = models.ManyToManyField('MODULE.picture')
      results = models.CommaSeparatedIntegerField(max_length=20)
      ...
    

    Also, by definition you can't have both a one-to-one and a many-to-many. I think you want the relationship on the Picture class to be a foreignkey not a one-to-one.

    0 讨论(0)
  • 2021-01-27 15:02

    You can pass in the string of the class name to the definition of OneToOne and ForeignKey and have it evaluated "lazily".

    competitors = models.ManyToManyField('picture')
    

    Also, the convention for model class names is camel-case (e.g. 'picture' => 'Picture', 'event' => 'Event').

    0 讨论(0)
提交回复
热议问题