Skip to main content



Django Query Expression


An F() object represents the value of a model field, transformed value of a model field, or annotated column. It makes it possible to refer to model field values and perform database operations using them without actually having to pull them out of the database into Python memory.

Another useful benefit of F() is that having the database - rather than Python - update a field’s value avoids a race condition.

When referencing relational fields such as ForeignKey, F() returns the primary key value rather than a model instance.


An aggregate expression is a special case of a Func() expression that informs the query that a GROUP BY clause is required. All of the aggregate functions, like Sum() and Count(), inherit from Aggregate().




Unlike aggregate(), annotate() is not a terminal clause. The output of the annotate() clause is a QuerySet; this QuerySet can be modified using any other QuerySet operation, including filter(), order_by(), or even additional calls to annotate().

Combining multiple aggregations with annotate() will yield the wrong results because joins are used instead of subqueries.

Django Cookiecutter