Topic guide Database access optimization
QuerySet.explain() gives you detailed information about how the database executes a query, including indexes and joins that are used.
769 sections in Django dev Search all versions →
Topic guide Database access optimization
QuerySet.explain() gives you detailed information about how the database executes a query, including indexes and joins that are used.
Reference The contenttypes framework
classGenericPrefetch(lookup,querysets,to_attr=None)[source] This lookup is similar to Prefetch() and it should only be used on GenericForeignKey. The querysets argument accepts a list of querysets, each for a different ContentType. This is useful for GenericForeignKey with non-homogeneous set of results.
Reference PostgreSQL specific model fields
The contains lookup is overridden on ArrayField. The returned objects will be those where the values passed are a subset of the data. It uses the SQL operator @>. For example:
Reference PostgreSQL specific model fields
The contains lookup is overridden on HStoreField. The returned objects are those where the given dict of key-value pairs are all contained in the field. It uses the SQL operator @>. For example:
Reference Databases
When using QuerySet.iterator(), Django opens a server-side cursor. By default, PostgreSQL assumes that only the first 10% of the results of cursor queries will be fetched.
Topic guide Making queries
The QuerySet returned by all() describes all objects in the database table. Usually, though, you’ll need to select only a subset of the complete set of objects. To create such a subset, you refine the initial QuerySet, adding filter conditions.
Topic guide Making queries
The result of refining a QuerySet is itself a QuerySet, so it’s possible to chain refinements together. For example: This takes the initial QuerySet of all entries in the database, adds a filter, then an exclusion, then another filter.
Reference PostgreSQL specific database constraints
ExclusionConstraint.condition A Q object that specifies the condition to restrict a constraint to a subset of rows. For example, condition=Q(cancelled=False). These conditions have the same database restrictions as django.db.models.Index.condition.
Reference Constraints reference
CheckConstraint.condition A Q object or boolean Expression that specifies the conditional check you want the constraint to enforce. For example: ensures the age field is never less than 18.
Reference Constraints reference
UniqueConstraint.condition A Q object that specifies the condition you want the constraint to enforce. For example: ensures that each user only has one draft. These conditions have the same database restrictions as Index.condition.
Topic guide Making queries
The contains lookup is overridden on JSONField. The returned objects are those where the given dict of key-value pairs are all contained in the top-level of the field. For example: NOTE: contains is not supported on Oracle and SQLite.
Tutorial Writing your first Django app, part 5
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.
Reference Model class reference
exceptionModel.MultipleObjectsReturned : This exception is raised by QuerySet.get() when multiple objects are found for the given lookups.
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.
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.
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.
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.
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.
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.
Reference PostgreSQL specific model fields
Range fields support the standard lookups: lt, gt, lte and gte. These are not particularly helpful - they compare the lower bounds first and then the upper bounds only if necessary.