Topic guide Managers Custom managersCreating a manager with QuerySet methods
classmethodfrom_queryset(queryset_class) For advanced usage you might want both a custom Manager and a custom QuerySet.
1413 sections across all versions Narrow to Django 6.0 (current) →
Topic guide Managers Custom managersCreating a manager with QuerySet methods
classmethodfrom_queryset(queryset_class) For advanced usage you might want both a custom Manager and a custom QuerySet.
Topic guide Database access optimization
Understanding QuerySets is vital to getting good performance with simple code. In particular:
Topic guide Database access optimization Understand QuerySets
To avoid performance problems, it is important to understand: that QuerySets are lazy. when they are evaluated. how the data is held in memory.
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.
Topic guide Database access optimization Don’t retrieve things you don’t need
When you only want a dict or list of values, and don’t need ORM model objects, make appropriate usage of values().
Topic guide Database access optimization Don’t retrieve things you don’t need
Use defer() and only() if there are database columns you know that you won’t need (or won’t need in most cases) to avoid loading them.
Topic guide Database access optimization Don’t retrieve things you don’t need
…if you only want to find out if obj is in the queryset, rather than if obj in queryset.
Topic guide Database access optimization Don’t retrieve things you don’t need
…if you only want the count, rather than doing len(queryset).
Topic guide Database access optimization Don’t retrieve things you don’t need
…if you only want to find out if at least one result exists, rather than if queryset. But:
Topic guide Database access optimization Don’t retrieve things you don’t need
Rather than retrieve a load of objects, set some values, and save them individual, use a bulk SQL UPDATE statement, via QuerySet.update(). Similarly, do bulk deletes where possible.
Topic guide Making queries Retrieving objectsRetrieving specific objects with filters
Each time you refine a QuerySet, you get a brand-new QuerySet that is in no way bound to the previous QuerySet. Each refinement creates a separate and distinct QuerySet that can be stored, used and reused.
Topic guide Making queries Retrieving objects
However, that’s far from all there is; see the QuerySet API Reference for a complete list of all the various QuerySet methods.
Topic guide Making queries Retrieving objects
Use a subset of Python’s array-slicing syntax to limit your QuerySet to a certain number of results. This is the equivalent of SQL’s LIMIT and OFFSET clauses.
Topic guide Making queries Retrieving objectsCaching and QuerySets
Querysets do not always cache their results. When evaluating only part of the queryset, the cache is checked, but if it is not populated then the items returned by the subsequent query are not cached.
Reference Databases
… | | X | | NO KEY | | | When using select_for_update() on MySQL, make sure you filter a queryset against at least a set of fields contained in unique constraints or only against fields covered by indexes. Otherwise, …
Reference Databases
There are special considerations described in Isolation In SQLite when modifying a table while iterating over it using QuerySet.iterator().
Topic guide Aggregation
Django provides two ways to generate aggregates. The first way is to generate summary values over an entire QuerySet. For example, say you wanted to calculate the average price of all books available for sale.
Topic guide Aggregation
Topic guide Aggregation
When an aggregation is applied to an empty queryset or grouping, the result defaults to its default parameter, typically None. This behavior occurs because aggregate functions return NULL when the executed query returns no rows.
Topic guide Managers
While most methods from the standard QuerySet are accessible directly from the Manager, this is only the case for the extra methods defined on a custom QuerySet if you also implement them on the Manager: This example allows you to …