mirror of
https://github.com/django/django.git
synced 2025-08-04 02:48:35 +00:00
Fixed #10929 -- Added default argument to aggregates.
Thanks to Simon Charette and Adam Johnson for the reviews.
This commit is contained in:
parent
59942a66ce
commit
501a8db465
11 changed files with 393 additions and 64 deletions
|
@ -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::
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue