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.
251 sections in Django 5.2 Search all versions →
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 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 QuerySet APIMethods that return new QuerySets
… the restaurants, one for the ‘best pizzas’, and one for the toppings. The best_pizza relationship could also be fetched using select_related to reduce the query count to 2: Since the prefetch is executed after the main query (which includes the …
Reference QuerySet API reference QuerySet APIMethods that return new QuerySets
… with the optional queryset argument. This can be used to change the default ordering of the queryset: Or to call select_related() when applicable to reduce the number of queries even further: You can also assign the prefetched result to a …
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 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
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.
… 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
The daily_cleanup.py script will be removed. The depth keyword argument will be removed from select_related(). The undocumented get_warnings_state()/restore_warnings_state() functions from django.test.utils and the save_warnings_state()/ restore_warnings_state() django.test.*TestCase methods are deprecated. Use the warnings.catch_warnings context manager available starting with Python 2.6 instead.
Reference System check framework contrib app checksadmin
admin.E117: The value of list_select_related must be a boolean, tuple or list. admin.E118: The value of list_per_page must be an integer. admin.E119: The value of list_max_show_all must be an integer.
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 Model instance reference
If you delete a field from a model instance, accessing it again reloads the value from the database: Model.refresh_from_db(using=None,fields=None,from_queryset=None)[source] Model.arefresh_from_db(using=None,fields=None,from_queryset=None) Asynchronous version: arefresh_from_db() If you need to reload a model’s values from the database, you can use the refresh_from_db() method.
Reference QuerySet API reference QuerySet APIMethods that return new QuerySets
… will still be deferred). You can defer loading of fields in related models (if the related models are loading via select_related()) by using the standard double-underscore notation to separate related fields: If you want to clear the set of deferred …
Reference QuerySet API reference QuerySet APIMethods that return new QuerySets
Using only() and omitting a field requested using select_related() is an error as well. On the other hand, invoking only() without any arguments, will return every field (including annotations) fetched by the queryset.
… (OGRGeometry method) - get_list_display() (ModelAdmin method) - get_list_display_links() (ModelAdmin method) - get_list_filter() (ModelAdmin method) - get_list_or_404() (in module django.shortcuts) - get_list_select_related() (ModelAdmin method) - get_log_entries() (AdminSite method) - get_login_url() (AccessMixin method) - (LoginRequiredMiddleware method) - get_lookup() (in module django.db.models) - …
… (ModelAdmin attribute) | - list_editable (ModelAdmin attribute) - list_filter (ModelAdmin attribute) - list_max_show_all (ModelAdmin attribute) - list_per_page (ModelAdmin attribute) - list_select_related (ModelAdmin attribute) - listdir() (Storage method) - lists() (QueryDict method) - ListView (built-in class) - LiveServerTestCase (class in django.test) …
Release notes Django 1.10 release notes Backwards incompatible changes in 1.10
Django 1.8 added validation for non-relational fields in select_related(): But it didn’t prohibit nested non-relation fields as it does now: