Topic guide Database access optimization Understand QuerySets
QuerySet.explain() gives you detailed information about how the database executes a query, including indexes and joins that are used.
766 sections in Django dev Search all versions →
Topic guide Database access optimization Understand QuerySets
QuerySet.explain() gives you detailed information about how the database executes a query, including indexes and joins that are used.
Reference The contenttypes framework Generic relations
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 Range FieldsQuerying Range FieldsContainment functions
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 Range FieldsQuerying Range FieldsContainment functions
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 PostgreSQL notes
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 Retrieving objects
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 Retrieving objectsRetrieving specific objects with filters
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
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 UniqueConstraint
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
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 Querying JSONFieldContainment and key lookups
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 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.
Reference Model class reference Attributes
exceptionModel.MultipleObjectsReturned : This exception is raised by QuerySet.get() when multiple objects are found for the given lookups.
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.
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.
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.
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:
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.
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.
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.