Take the 2-minute tour ×

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

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

I understand (I think...) that (1) MyModel needs to be defined in that same file and the other needs to be imported, but I'm unsure of the reason/benifits of doing it either way.

I had a look through the Django docs but couldnt find anything, and Im also not sure if this is the right place to ask, so apologies if not.


share|improve this question

2 Answers 2

up vote 2 down vote accepted

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).

share|improve this answer

Without quotes, it's a reference to a model either defined within the file or imported via import. With quotes, Django is made responsible for finding the model among all the models in all installed apps.

share|improve this answer
Does that mean with quotes, you dont need to import the model? –  neolaser Jan 17 '11 at 0:01
Yes. But it takes a (very) little longer for Django to resolve it rather than having Python do it. –  Ignacio Vazquez-Abrams Jan 17 '11 at 0:03

Your Answer


