djangodocs.org

743 sections in Django 6.0 Search all versions →

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.

Other QuerySet methods

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.

Limiting QuerySets

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 MySQL notes

… | | 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, …

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 Aggregations and other QuerySet clauses

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 Custom 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 …

Topic guide Managers Custom managers

In lieu of the above approach which requires duplicating methods on both the QuerySet and the Manager, QuerySet.as_manager() can be used to create an instance of Manager with a copy of a custom QuerySet’s methods: The Manager instance created by …

Topic guide Multiple databases Manually selecting a databaseUsing managers with multiple databases

If you’re overriding get_queryset() on your manager, be sure to either call the method on the parent (using super()) or do the appropriate handling of the _db attribute on the manager (a string containing the name of the database to …

Caching and QuerySets

Topic guide Making queries Retrieving objects

Each QuerySet contains a cache to minimize database access. Understanding how it works will allow you to write the most efficient code. In a newly created QuerySet, the cache is empty.

Changing the queryset

Topic guide Creating forms from models Model formsets

By default, when you create a formset from a model, the formset will use a queryset that includes all objects in the model (e.g., Author.objects.all()).

Using a custom queryset

Topic guide Creating forms from models Model formsets

As stated earlier, you can override the default queryset used by the model formset: Note that we pass the queryset argument in both the POST and GET cases in this example.