djangodocs.org

680 sections in Django 5.0 Search all versions →

Avg

Reference QuerySet API reference QuerySet APIAggregation functions

classAvg(expression,output_field=None,distinct=False,filter=None,default=None,**extra) : Returns the mean value of the given expression, which must be numeric unless you specify a different output_field.

Max

Reference QuerySet API reference QuerySet APIAggregation functions

classMax(expression,output_field=None,filter=None,default=None,**extra) : Returns the maximum value of the given expression.

Min

Reference QuerySet API reference QuerySet APIAggregation functions

classMin(expression,output_field=None,filter=None,default=None,**extra) : Returns the minimum value of the given expression.

StdDev

Reference QuerySet API reference QuerySet APIAggregation functions

classStdDev(expression,output_field=None,sample=False,filter=None,default=None,**extra) : Returns the standard deviation of the data in the provided expression.

Sum

Reference QuerySet API reference QuerySet APIAggregation functions

classSum(expression,output_field=None,distinct=False,filter=None,default=None,**extra) : Computes the sum of all values of the given expression.

Variance

Reference QuerySet API reference QuerySet APIAggregation functions

classVariance(expression,output_field=None,sample=False,filter=None,default=None,**extra) : Returns the variance of the data in the provided expression.

Prefetch() objects

Reference QuerySet API reference Query-related tools

classPrefetch(lookup,queryset=None,to_attr=None) 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) 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: Changed in Django 5.0: Support for using the filter argument was added.

Extent3D

Reference GIS QuerySet API Reference Distance LookupsAggregate Functions

classExtent3D(geo_field,filter=None) 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).

MakeLine

Reference GIS QuerySet API Reference Distance LookupsAggregate Functions

classMakeLine(geo_field,filter=None) Availability: PostGIS, SpatiaLite Returns a LineString constructed from the point field geometries in the QuerySet. Currently, ordering the queryset has no effect. Example: Changed in Django 5.0: Support for using the filter argument was added.

Union

Reference GIS QuerySet API Reference Distance LookupsAggregate Functions

classUnion(geo_field,filter=None) 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.

Subquery() expressions

Reference Query Expressions Built-in Expressions

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

Exists() subqueries

Reference Query Expressions Built-in ExpressionsSubquery() expressions

classExists(queryset) 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.

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:

contains

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:

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.