Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between select_related() and select_related('columnname') in django

Tags:

python

django

I am sorry for this novice question. I tried to find out difference between select_related() and select_related('ColumnName'). Is there difference between these two ?

I am trying to understand this post in which author emphasizing to use select_related('content_type').all() rather than simple select_related().all(). Your help will be appreciated since from last two hours I am struggling to grasp this concept.

Thank you very much,

Sunil

like image 829
SRC Avatar asked Jul 03 '12 18:07

SRC


1 Answers

From the django docs, when called with no arguments:

Returns a QuerySet that will automatically "follow" foreign-key relationships, selecting that additional related-object data when it executes its query.

When given an argument:

Sometimes you only want to access specific models that are related to your root model, not all of the related models. In these cases, you can pass the related field names to select_related() and it will only follow those relations. You can even do this for models that are more than one relation away by separating the field names with double underscores, just as for filters.

like image 125
jterrace Avatar answered Sep 30 '22 16:09

jterrace