Aggregation functions
Currently 6 aggregation functions are supported.
count(distinct: bool = True) -> intexists() -> boolsum(columns) -> Anyavg(columns) -> Anymin(columns) -> Any-
max(columns) -> Any -
QuerysetProxyQuerysetProxy.count(distinct=True)methodQuerysetProxy.exists()methodQuerysetProxy.sum(columns)methodQuerysetProxy.avg(columns)methodQuerysetProxy.min(column)methodQuerysetProxy.max(columns)method
count
count(distinct: bool = True) -> int
Returns number of rows matching the given criteria (i.e. applied with filter and exclude).
If distinct is True (the default), this will return the number of primary rows selected. If False,
the count will be the total number of rows returned
(including extra rows for one-to-many or many-to-many left select_related table joins).
False is the legacy (buggy) behavior for workflows that depend on it.
| Python | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
| Python | |
|---|---|
1 2 | |
exists
exists() -> bool
Returns a bool value to confirm if there are rows matching the given criteria (applied with filter and exclude)
| Python | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
| Python | |
|---|---|
1 2 | |
sum
sum(columns) -> Any
Returns sum value of columns for rows matching the given criteria (applied with filter and exclude if set before).
You can pass one or many column names including related columns.
As of now each column passed is aggregated separately (so sum(col1+col2) is not possible,
you can have sum(col1, col2) and later add 2 returned sums in python)
You cannot sum non numeric columns.
If you aggregate on one column, the single value is directly returned as a result If you aggregate on multiple columns a dictionary with column: result pairs is returned
Given models like follows
| Python | |
|---|---|
1 | |
A sample usage might look like following
| Python | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | |
avg
avg(columns) -> Any
Returns avg value of columns for rows matching the given criteria (applied with filter and exclude if set before).
You can pass one or many column names including related columns.
As of now each column passed is aggregated separately (so sum(col1+col2) is not possible,
you can have sum(col1, col2) and later add 2 returned sums in python)
You cannot avg non numeric columns.
If you aggregate on one column, the single value is directly returned as a result If you aggregate on multiple columns a dictionary with column: result pairs is returned
| Python | |
|---|---|
1 | |
A sample usage might look like following
| Python | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | |
min
min(columns) -> Any
Returns min value of columns for rows matching the given criteria (applied with filter and exclude if set before).
You can pass one or many column names including related columns.
As of now each column passed is aggregated separately (so sum(col1+col2) is not possible,
you can have sum(col1, col2) and later add 2 returned sums in python)
If you aggregate on one column, the single value is directly returned as a result If you aggregate on multiple columns a dictionary with column: result pairs is returned
| Python | |
|---|---|
1 | |
A sample usage might look like following
| Python | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |
max
max(columns) -> Any
Returns max value of columns for rows matching the given criteria (applied with filter and exclude if set before).
Returns min value of columns for rows matching the given criteria (applied with filter and exclude if set before).
You can pass one or many column names including related columns.
As of now each column passed is aggregated separately (so sum(col1+col2) is not possible,
you can have sum(col1, col2) and later add 2 returned sums in python)
If you aggregate on one column, the single value is directly returned as a result If you aggregate on multiple columns a dictionary with column: result pairs is returned
| Python | |
|---|---|
1 | |
A sample usage might look like following
| Python | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |
QuerysetProxy methods
When access directly the related ManyToMany field as well as ReverseForeignKey
returns the list of related models.
But at the same time it exposes a subset of QuerySet API, so you can filter, create, select related etc related models directly from parent model.
count
Works exactly the same as count function above but allows you to select columns from related objects from other side of the relation.
Tip
To read more about QuerysetProxy visit querysetproxy section
exists
Works exactly the same as exists function above but allows you to select columns from related objects from other side of the relation.
sum
Works exactly the same as sum function above but allows you to sum columns from related objects from other side of the relation.
avg
Works exactly the same as avg function above but allows you to average columns from related objects from other side of the relation.
min
Works exactly the same as min function above but allows you to select minimum of columns from related objects from other side of the relation.
max
Works exactly the same as max function above but allows you to select maximum of columns from related objects from other side of the relation.
Tip
To read more about QuerysetProxy visit querysetproxy section