Django: Why are quotes around the model in a ForeignKey definition

后端 未结 3 1308
一向
一向 2020-12-28 21:51

I want to know what the difference between these two foreignkey definitions are.

(1) MyFKField = models.ForeignKey(\'MyModel\')
(2) MyFKField = models.Foreig         


        
3条回答
  •  借酒劲吻你
    2020-12-28 22:31

    Django docs states that you would use a string to (1):

    • You want a recursive relationship (eg - model.ForeignKey('self'))
    • For referring to a model that is possibly not defined yet (for cyclic relationships).
    • A shortcut to refer to a model in another application (eg - model.ForeignKey('app.mymodel'))

    But in general, specifying the model class directly is clear where it's coming from (2).

提交回复
热议问题