diff options
| author | Simon Charette <charette.s@gmail.com> | 2021-05-21 22:32:16 -0400 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2021-07-02 07:25:42 +0200 |
| commit | 9f3cce172f6913c5ac74272fa5fc07f847b4e112 (patch) | |
| tree | 027dec2a7a1bef8c02c2e58ffded8718401b28d1 /docs | |
| parent | f3112fde981052801e0c2121027424496c03efdf (diff) | |
Refs #26430 -- Re-introduced empty aggregation optimization.
The introduction of the Expression.empty_aggregate_value interface
allows the compilation stage to enable the EmptyResultSet optimization
if all the aggregates expressions implement it.
This also removes unnecessary RegrCount/Count.convert_value() methods.
Disabling the empty result set aggregation optimization when it wasn't
appropriate prevented None returned for a Count aggregation value.
Thanks Nick Pope for the review.
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/ref/models/expressions.txt | 17 | ||||
| -rw-r--r-- | docs/releases/4.0.txt | 3 |
2 files changed, 20 insertions, 0 deletions
diff --git a/docs/ref/models/expressions.txt b/docs/ref/models/expressions.txt index 9ab502d244..d45cfbe00a 100644 --- a/docs/ref/models/expressions.txt +++ b/docs/ref/models/expressions.txt @@ -410,6 +410,14 @@ The ``Aggregate`` API is as follows: allows passing a ``distinct`` keyword argument. If set to ``False`` (default), ``TypeError`` is raised if ``distinct=True`` is passed. + .. attribute:: empty_aggregate_value + + .. versionadded:: 4.0 + + Override :attr:`~django.db.models.Expression.empty_aggregate_value` to + ``None`` since most aggregate functions result in ``NULL`` when applied + to an empty result set. + The ``expressions`` positional arguments can include expressions, transforms of the model field, or the names of model fields. They will be converted to a string and used as the ``expressions`` placeholder within the ``template``. @@ -950,6 +958,15 @@ calling the appropriate methods on the wrapped expression. in :class:`~django.db.models.expressions.Window`. Defaults to ``False``. + .. attribute:: empty_aggregate_value + + .. versionadded:: 4.0 + + Tells Django which value should be returned when the expression is used + to :meth:`aggregate <django.db.models.query.QuerySet.aggregate>` over + an empty result set. Defaults to :py:data:`NotImplemented` which forces + the expression to be computed on the database. + .. method:: resolve_expression(query=None, allow_joins=True, reuse=None, summarize=False, for_save=False) Provides the chance to do any pre-processing or validation of diff --git a/docs/releases/4.0.txt b/docs/releases/4.0.txt index a9b29d7ce4..c1e64c499a 100644 --- a/docs/releases/4.0.txt +++ b/docs/releases/4.0.txt @@ -265,6 +265,9 @@ Models * :meth:`.QuerySet.bulk_update` now returns the number of objects updated. +* The new :attr:`.Aggregate.empty_aggregate_value` attribute allows specifying + a value to return when the aggregation is used over an empty result set. + Requests and Responses ~~~~~~~~~~~~~~~~~~~~~~ |
