Hi Mohit,

The only situations where I have used strings are forward declarations or 
potential reference loops.

Intuitively I'd say that referencing models by their name adds a slight 
overhead for class lookup. But since this is supposed to happen only when the 
model definition is loaded (fields are class level attributes), there are 
chances that there is no impact on performances during the application normal 
operation. The impact should be on the application start phase, but it's 
probably neglectable compared to the overall start time.

Since I've never done any real timing about this point, nor studied Django 
source code to identify the real mechanism, I'm strongly interested in other 
opinions... especially if my analysis is wrong 😉

Best

Eric

________________________________
From: django-users@googlegroups.com <django-users@googlegroups.com> on behalf 
of Mohit Solanki <mohitsolanki...@gmail.com>
Sent: Sunday, April 7, 2019 05:43
To: Django users
Subject: Specifying model relationship as string vs concrete model?

Since Django provides two ways of specifying model relationship,

models.ForeignKey('User') vs models.ForeignKey(User)

Which one is more preferred and recommended? Is there any upside or downside of 
choosing one over the other?  In Django documentation, all the example are of 
the second form but I have seen many people specifying the relationship as a 
string. Can Someone please elaborate more on this?

Regards,

Mohit

--
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to 
django-users+unsubscr...@googlegroups.com<mailto:django-users+unsubscr...@googlegroups.com>.
To post to this group, send email to 
django-users@googlegroups.com<mailto:django-users@googlegroups.com>.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/06988f5c-9ad0-47b1-9216-5f689abb1720%40googlegroups.com<https://groups.google.com/d/msgid/django-users/06988f5c-9ad0-47b1-9216-5f689abb1720%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/AM0P193MB0308A3049D2828BB316C4B348C530%40AM0P193MB0308.EURP193.PROD.OUTLOOK.COM.
For more options, visit https://groups.google.com/d/optout.

Reply via email to