djangodocs.org

769 sections in Django dev Search all versions →

GenericPrefetch()

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.

contains

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:

contains

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:

Server-side cursors

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.

Chaining filters

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.

condition

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.

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.

condition

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.

contains

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.

Using F() in filters

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 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.

Comparison functions

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.