djangodocs.org

743 sections across all versions Narrow to Django 6.0 (current) →

Prefetch() objects

Reference QuerySet API reference Query-related tools

classPrefetch(lookup,queryset=None,to_attr=None)[source] The Prefetch() object can be used to control the operation of prefetch_related(). The lookup argument describes the relations to follow and works the same as the string based lookups passed to prefetch_related().

Reference QuerySet API reference Query-related tools

This is useful in code that receives a list of model instances as opposed to a QuerySet; for example, when fetching models from a cache or instantiating them manually.

Reference QuerySet API reference Query-related tools

… find restaurants that have vegetarian pizzas with 'mozzarella' in the name: If there are a large number of pizzas, this queryset performs better than: because the filtering in the WHERE clause of the first queryset will only operate on vegetarian …

Extent

Reference GIS QuerySet API Reference Distance LookupsAggregate Functions

classExtent(geo_field,filter=None)[source] Availability: PostGIS, Oracle, SpatiaLite Returns the extent of all geo_field in the QuerySet as a 4-tuple, comprising the lower left coordinate and the upper right coordinate. Example:

Extent3D

Reference GIS QuerySet API Reference Distance LookupsAggregate Functions

classExtent3D(geo_field,filter=None)[source] Availability: PostGIS Returns the 3D extent of all geo_field in the QuerySet as a 6-tuple, comprising the lower left coordinate and upper right coordinate (each with x, y, and z coordinates). Example:

MakeLine

Reference GIS QuerySet API Reference Distance LookupsAggregate Functions

classMakeLine(geo_field,filter=None)[source] Availability: PostGIS, SpatiaLite Returns a LineString constructed from the point field geometries in the QuerySet. Currently, ordering the queryset has no effect. Example:

Union

Reference GIS QuerySet API Reference Distance LookupsAggregate Functions

classUnion(geo_field,filter=None)[source] Availability: PostGIS, Oracle, SpatiaLite This method returns a GEOSGeometry object comprising the union of every geometry in the queryset. Please note that use of Union is processor intensive and may take a significant amount of time on large querysets.

ModelAdmin methods

Reference The Django admin site ModelAdmin objects

ModelAdmin.formfield_for_manytomany(db_field,request,**kwargs) : Like the formfield_for_foreignkey method, the formfield_for_manytomany method can be overridden to change the default formfield for a many to many field.

Subquery() expressions

Reference Query Expressions Built-in Expressions

classSubquery(queryset,output_field=None)[source] You can add an explicit subquery to a QuerySet using the Subquery expression.

Exists() subqueries

Reference Query Expressions Built-in ExpressionsSubquery() expressions

classExists(queryset)[source] Exists is a Subquery subclass that uses an SQL EXISTS statement. In many cases it will perform better than a subquery since the database is able to stop evaluation of the subquery when a first matching row is found.

Use explain()

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.

GenericPrefetch()

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.

5.0 5.1 5.2 6.0 dev

contains

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:

Server-side cursors

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.

Chaining filters

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.

condition

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.

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.

5.1 5.2 6.0 dev

contains

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.