Fixed #10929 -- Added default argument to aggregates.

Thanks to Simon Charette and Adam Johnson for the reviews.
This commit is contained in:
Nick Pope 2021-02-21 01:38:55 +00:00 committed by Mariusz Felisiak
parent 59942a66ce
commit 501a8db465
11 changed files with 393 additions and 64 deletions

View file

@ -59,7 +59,7 @@ will result in a database error.
Usage examples::
>>> # Get a screen name from least to most public
>>> from django.db.models import Sum, Value as V
>>> from django.db.models import Sum
>>> from django.db.models.functions import Coalesce
>>> Author.objects.create(name='Margaret Smith', goes_by='Maggie')
>>> author = Author.objects.annotate(
@ -68,13 +68,18 @@ Usage examples::
Maggie
>>> # Prevent an aggregate Sum() from returning None
>>> # The aggregate default argument uses Coalesce() under the hood.
>>> aggregated = Author.objects.aggregate(
... combined_age=Coalesce(Sum('age'), V(0)),
... combined_age_default=Sum('age'))
... combined_age=Sum('age'),
... combined_age_default=Sum('age', default=0),
... combined_age_coalesce=Coalesce(Sum('age'), 0),
... )
>>> print(aggregated['combined_age'])
0
>>> print(aggregated['combined_age_default'])
None
>>> print(aggregated['combined_age_default'])
0
>>> print(aggregated['combined_age_coalesce'])
0
.. warning::

View file

@ -393,7 +393,7 @@ some complex computations::
The ``Aggregate`` API is as follows:
.. class:: Aggregate(*expressions, output_field=None, distinct=False, filter=None, **extra)
.. class:: Aggregate(*expressions, output_field=None, distinct=False, filter=None, default=None, **extra)
.. attribute:: template
@ -452,6 +452,11 @@ The ``filter`` argument takes a :class:`Q object <django.db.models.Q>` that's
used to filter the rows that are aggregated. See :ref:`conditional-aggregation`
and :ref:`filtering-on-annotations` for example usage.
The ``default`` argument takes a value that will be passed along with the
aggregate to :class:`~django.db.models.functions.Coalesce`. This is useful for
specifying a value to be returned other than ``None`` when the queryset (or
grouping) contains no entries.
The ``**extra`` kwargs are ``key=value`` pairs that can be interpolated
into the ``template`` attribute.
@ -459,6 +464,10 @@ into the ``template`` attribute.
Support for transforms of the field was added.
.. versionchanged:: 4.0
The ``default`` argument was added.
Creating your own Aggregate Functions
-------------------------------------

View file

@ -3540,8 +3540,10 @@ documentation to learn how to create your aggregates.
Aggregation functions return ``None`` when used with an empty
``QuerySet``. For example, the ``Sum`` aggregation function returns ``None``
instead of ``0`` if the ``QuerySet`` contains no entries. An exception is
``Count``, which does return ``0`` if the ``QuerySet`` is empty.
instead of ``0`` if the ``QuerySet`` contains no entries. To return another
value instead, pass a value to the ``default`` argument. An exception is
``Count``, which does return ``0`` if the ``QuerySet`` is empty. ``Count``
does not support the ``default`` argument.
All aggregates have the following parameters in common:
@ -3578,6 +3580,16 @@ rows that are aggregated.
See :ref:`conditional-aggregation` and :ref:`filtering-on-annotations` for
example usage.
.. _aggregate-default:
``default``
~~~~~~~~~~~
.. versionadded:: 4.0
An optional argument that allows specifying a value to use as a default value
when the queryset (or grouping) contains no entries.
``**extra``
~~~~~~~~~~~
@ -3587,7 +3599,7 @@ by the aggregate.
``Avg``
~~~~~~~
.. class:: Avg(expression, output_field=None, distinct=False, filter=None, **extra)
.. class:: Avg(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``.
@ -3623,10 +3635,14 @@ by the aggregate.
This is the SQL equivalent of ``COUNT(DISTINCT <field>)``. The default
value is ``False``.
.. note::
The ``default`` argument is not supported.
``Max``
~~~~~~~
.. class:: Max(expression, output_field=None, filter=None, **extra)
.. class:: Max(expression, output_field=None, filter=None, default=None, **extra)
Returns the maximum value of the given expression.
@ -3636,7 +3652,7 @@ by the aggregate.
``Min``
~~~~~~~
.. class:: Min(expression, output_field=None, filter=None, **extra)
.. class:: Min(expression, output_field=None, filter=None, default=None, **extra)
Returns the minimum value of the given expression.
@ -3646,7 +3662,7 @@ by the aggregate.
``StdDev``
~~~~~~~~~~
.. class:: StdDev(expression, output_field=None, sample=False, filter=None, **extra)
.. class:: StdDev(expression, output_field=None, sample=False, filter=None, default=None, **extra)
Returns the standard deviation of the data in the provided expression.
@ -3664,7 +3680,7 @@ by the aggregate.
``Sum``
~~~~~~~
.. class:: Sum(expression, output_field=None, distinct=False, filter=None, **extra)
.. class:: Sum(expression, output_field=None, distinct=False, filter=None, default=None, **extra)
Computes the sum of all values of the given expression.
@ -3682,7 +3698,7 @@ by the aggregate.
``Variance``
~~~~~~~~~~~~
.. class:: Variance(expression, output_field=None, sample=False, filter=None, **extra)
.. class:: Variance(expression, output_field=None, sample=False, filter=None, default=None, **extra)
Returns the variance of the data in the provided expression.