Django: Why quotation marks around a model in a ForeignKey definition

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

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

I understand (I think ...) that (1) MyModelneeds to be defined in the same file and the other needs to be imported, but I'm not sure about the reason / advantages of this anyway.

I looked through the Django docs but couldn't find anything, and I'm also not sure if this is the right place to request, so apologize if not.

Greetings

+5
source share
3 answers

Django docs states that you should use a string for (1):

  • A recursive relation is required (e.g. - model.ForeignKey('self'))
  • , , , ( ).
  • (, model.ForeignKey('app.mymodel'))

, , , (2).

+5

, import. Django .

+6

MyModel MyFKField ( ), .

0

All Articles