mirror of https://github.com/django/django.git
[2.2.x] Fixed #30486 -- Fixed the default value of Aggregate.distinct and updated example of custom aggregate functions.
Backport of 76b3fc5c8d
from master
This commit is contained in:
parent
d5d22e1090
commit
36766e1a28
|
@ -381,7 +381,7 @@ The ``Aggregate`` API is as follows:
|
||||||
|
|
||||||
A class attribute, as a format string, that describes the SQL that is
|
A class attribute, as a format string, that describes the SQL that is
|
||||||
generated for this aggregate. Defaults to
|
generated for this aggregate. Defaults to
|
||||||
``'%(function)s( %(expressions)s )'``.
|
``'%(function)s(%(distinct)s%(expressions)s)'``.
|
||||||
|
|
||||||
.. attribute:: function
|
.. attribute:: function
|
||||||
|
|
||||||
|
@ -444,20 +444,19 @@ SQL that is generated. Here's a brief example::
|
||||||
|
|
||||||
from django.db.models import Aggregate
|
from django.db.models import Aggregate
|
||||||
|
|
||||||
class Count(Aggregate):
|
class Sum(Aggregate):
|
||||||
# supports COUNT(distinct field)
|
# Supports SUM(ALL field).
|
||||||
function = 'COUNT'
|
function = 'SUM'
|
||||||
template = '%(function)s(%(distinct)s%(expressions)s)'
|
template = '%(function)s(%(all_values)s%(expressions)s)'
|
||||||
|
allow_distinct = False
|
||||||
|
|
||||||
def __init__(self, expression, distinct=False, **extra):
|
def __init__(self, expression, all_values=False, **extra):
|
||||||
super().__init__(
|
super().__init__(
|
||||||
expression,
|
expression,
|
||||||
distinct='DISTINCT ' if distinct else '',
|
all_values='ALL ' if all_values else '',
|
||||||
output_field=IntegerField(),
|
|
||||||
**extra
|
**extra
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
``Value()`` expressions
|
``Value()`` expressions
|
||||||
-----------------------
|
-----------------------
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue