Topic guide
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 …
dev
Topic guide
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 …
dev
Topic guide
Making queries
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.
dev
Topic guide
Creating forms from models
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()).
dev
Topic guide
Creating forms from models
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.
dev
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, …
4.2
5.0
5.1
5.2
6.0
Reference
Databases
SQLite notes
There are special considerations described in Isolation In SQLite when modifying a table while iterating over it using QuerySet.iterator().
4.2
5.0
5.1
5.2
6.0
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.
4.2
5.0
5.1
5.2
6.0
Topic guide
Aggregation
4.2
5.0
5.1
5.2
6.0
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.
4.2
5.0
5.1
5.2
6.0
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 …
4.2
5.0
5.1
5.2
6.0
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 …
4.2
5.0
5.1
5.2
6.0
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 …
4.2
5.0
5.1
5.2
6.0
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.
4.2
5.0
5.1
5.2
6.0
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()).
4.2
5.0
5.1
5.2
6.0
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.
4.2
5.0
5.1
5.2
6.0
Reference
Query Expressions
classOuterRef(field)[source] Use OuterRef when a queryset in a Subquery needs to refer to a field from the outer query or its transform.
dev
Topic guide
Aggregation
The second way to generate summary values is to generate an independent summary for each object in a QuerySet.
dev
Topic guide
Managers
A Manager’s base QuerySet returns all objects in the system. For example, using this model: …the statement Book.objects.all() will return all books in the database. You can override a Manager’s base QuerySet by overriding the Manager.get_queryset() method.
dev
Topic guide
Multiple databases
You can select the database for a QuerySet at any point in the QuerySet “chain.” Call using() on the QuerySet to get another QuerySet that uses the specified database.
dev