Reference
Query Expressions
F() is also very useful in QuerySet filters, where they make it possible to filter a set of objects against criteria based on their field values, rather than on Python values. This is documented in using F() expressions in queries.
dev
Topic guide
Database access optimization
As well as caching of the whole QuerySet, there is caching of the result of attributes on ORM objects. In general, attributes that are not callable will be cached.
dev
Topic guide
Database access optimization
Where prefetch_related would be useful after the queryset has been evaluated, use prefetch_related_objects() to execute an extra prefetch.
dev
Topic guide
Making queries
filter() will always give you a QuerySet, even if only a single object matches the query - in this case, it will be a QuerySet containing a single element.
dev
Topic guide
Making queries
NOTE: The behavior of filter() for queries > that span multi-value relationships, as described above, is not implemented > equivalently for exclude(). Instead, > the conditions in a single exclude() > call will not necessarily refer to the same item.
dev
Topic guide
Making queries
In addition to the QuerySet methods defined in “Retrieving objects” above, the ForeignKey Manager has additional methods used to handle the set of related objects.
dev
Tutorial
Writing your first Django app, part 5
Test a view
We ought to add a similar get_queryset method to ResultsView and create a new test class for that view. It’ll be very similar to what we have just created; in fact there will be a lot of repetition.
4.2
5.0
5.1
5.2
6.0
Reference
Model class reference
Attributes
exceptionModel.MultipleObjectsReturned : This exception is raised by QuerySet.get() when multiple objects are found for the given lookups.
4.2
5.0
5.1
5.2
6.0
Reference
Query Expressions
Built-in ExpressionsF() expressions
F() is also very useful in QuerySet filters, where they make it possible to filter a set of objects against criteria based on their field values, rather than on Python values. This is documented in using F() expressions in queries.
4.2
5.0
5.1
5.2
6.0
Topic guide
Aggregation
Aggregations and other QuerySet clausesfilter() and exclude()
It’s more efficient to use QuerySet.filter() to exclude > rows. The aggregation filter argument is only useful when using two or > more aggregations over the same relations with different conditionals.
4.2
5.0
5.1
5.2
6.0
Topic guide
Aggregation
Aggregations and other QuerySet clausesfilter() and exclude()
When developing a complex query that involves both annotate() and filter() clauses, pay particular attention to the order in which the clauses are applied to the QuerySet.
4.2
5.0
5.1
5.2
6.0
Topic guide
Aggregation
Aggregations and other QuerySet clauses
For example, to order a QuerySet of books by the number of authors that have contributed to the book, you could use the following query:
4.2
5.0
5.1
5.2
6.0
Topic guide
Aggregation
Aggregations and other QuerySet clauses
Ordinarily, annotations are generated on a per-object basis - an annotated QuerySet will return one result for each object in the original QuerySet.
4.2
5.0
5.1
5.2
6.0
Topic guide
Aggregation
Aggregations and other QuerySet clausesvalues()
Fields that are mentioned in the order_by() part of a queryset are used when selecting the output data, even if they are not otherwise specified in the values() call.
4.2
5.0
5.1
5.2
6.0
Topic guide
Aggregation
Aggregations and other QuerySet clauses
When using the values() clause to group query results for annotations in MySQL with the ONLY_FULL_GROUP_BY SQL mode enabled, you may need to apply AnyValue if the annotation includes a mix of aggregate and non-aggregate expressions.
6.0
Topic guide
Database access optimization
Understand QuerySets
As well as caching of the whole QuerySet, there is caching of the result of attributes on ORM objects. In general, attributes that are not callable will be cached.
4.2
5.0
5.1
5.2
6.0
Topic guide
Database access optimization
Understand QuerySets
To make use of the caching behavior of QuerySet, you may need to use the with template tag.
4.2
5.0
5.1
5.2
6.0
Topic guide
Database access optimization
Understand QuerySets
When you have a lot of objects, the caching behavior of the QuerySet can cause a large amount of memory to be used. In this case, iterator() may help.
4.2
5.0
5.1
5.2
6.0
Topic guide
Making queries
Retrieving objects
filter() will always give you a QuerySet, even if only a single object matches the query - in this case, it will be a QuerySet containing a single element.
4.2
5.0
5.1
5.2
6.0
Topic guide
Making queries
Retrieving objectsLookups that span relationships
NOTE: The behavior of filter() for queries > that span multi-value relationships, as described above, is not implemented > equivalently for exclude(). Instead, > the conditions in a single exclude() > call will not necessarily refer to the same item.
4.2
5.0
5.1
5.2
6.0