Reference QuerySet API reference
select_related(*fields) Returns a QuerySet that will join in the named foreign-key relationships, selecting additional related objects when it executes its query.
477 sections across all versions Narrow to Django 6.0 (current) →
Reference QuerySet API reference
select_related(*fields) Returns a QuerySet that will join in the named foreign-key relationships, selecting additional related objects when it executes its query.
Reference QuerySet API reference QuerySet APIMethods that return new QuerySets
select_related(*fields) Returns a QuerySet that will “follow” foreign-key relationships, selecting additional related-object data when it executes its query.
Topic guide Database access optimization
When the FETCH_PEERS fetch mode is not appropriate or efficient enough, use select_related() and prefetch_related(). Understand their documentation thoroughly and apply them where needed. It may be useful to apply these methods in managers and default managers.
Topic guide Database access optimization Retrieve everything at once if you know you will need it
Understand select_related() and prefetch_related() thoroughly, and use them: in managers and default managers where appropriate. Be aware when your manager is and is not used; sometimes this is tricky so don’t make assumptions.
Reference QuerySet API reference QuerySet APIMethods that return new QuerySets
This has a similar purpose to select_related, in that both are designed to stop the deluge of database queries that is caused by accessing related objects, but the strategy is quite different.
Reference QuerySet API reference
This method has a similar purpose to select_related(), in that both are designed to eagerly fetch related objects. However, they work in different ways.
Reference QuerySet API reference
For example, rows of related objects specified in select_related() are locked in addition to rows of the queryset’s model. If this isn’t desired, specify the related objects you want to lock in select_for_update(of=(...)) using the same fields syntax as select_related().
Topic guide Making queries
These accessors may be prefetched by the QuerySet methods select_related() or prefetch_related(). If not prefetched, access will trigger an on-demand fetch through the model’s fetch mode.
Reference QuerySet API reference QuerySet APIMethods that return new QuerySets
For example, rows of related objects specified in select_related() are locked in addition to rows of the queryset’s model. If this isn’t desired, specify the related objects you want to lock in select_for_update(of=(...)) using the same fields syntax as select_related().
Reference The Django admin site ModelAdmin objects
By default, this is set to 100. ModelAdmin.list_select_related : Set list_select_related to tell Django to use select_related() in retrieving the list of objects on the admin change list page. This can save you a bunch of database queries.
Reference QuerySet API reference Query-related tools
… a base QuerySet for the given lookup. This is useful to further filter down the prefetch operation, or to call select_related() from the prefetched relation, hence reducing the number of queries even further: The to_attr argument sets the result of …
Topic guide Making queries Related objectsOne-to-many relationships
Subsequent accesses to the foreign key on the same object instance are cached. Example: Note that the select_related() QuerySet method recursively prepopulates the cache of all one-to-many relationships ahead of time. Example:
Design philosophies Database API
This is also why the select_related() QuerySet method exists. It’s an optional performance booster for the common case of selecting “every related object.”
Reference The Django admin site ModelAdmin objects
ModelAdmin.get_list_select_related(request)[source] : The get_list_select_related method is given the HttpRequest and should return a boolean or list as ModelAdmin.list_select_related does.
Reference QuerySet API reference
… a base QuerySet for the given lookup. This is useful to further filter down the prefetch operation, or to call select_related() from the prefetched relation, hence reducing the number of queries even further: The to_attr argument sets the result of …
Reference The Django admin site
By default, this is set to 100. ModelAdmin.list_select_related : Set list_select_related to tell Django to use select_related() in retrieving the list of objects on the admin change list page. This can save you a bunch of database queries.
Topic guide Database access optimization
Enable it for all usage of your models with a custom manager. Using this fetch mode is easier than declaring fields to fetch with select_related() or prefetch_related(), especially when it’s hard to predict which fields will be accessed.
Topic guide Making queries
Subsequent accesses to the foreign key on the same object instance are cached. Example: Note that the select_related() QuerySet method recursively prepopulates the cache of all one-to-many relationships ahead of time. Example:
… django.middleware.security) - seekable() (HttpResponse method) - Select (class in django.forms) - select_for_update() (in module django.db.models.query.QuerySet) - select_on_save (Options attribute) - select_related() (in module django.db.models.query.QuerySet) - select_template() (Engine method) - (in module django.template.loader) - SelectDateWidget (class in django.forms) - SelectMultiple (class …
Internals Django Deprecation Timeline
Calling QuerySet.select_related() without arguments will raise TypeError. QuerySet.select_related() will raise TypeError if passed True rather than AttributeError. ModelAdmin will raise ValueError if subclassed with its list_select_related attribute set to True. ModelAdmin will raise ValueError if its get_list_select_related() method returns True.