aggregate(*args, **kwargs)
Returns a dictionary of aggregate values (averages, sums, etc.) calculated over the QuerySet
. Each argument to aggregate()
specifies a value that will be included in the dictionary that is returned.
The aggregation functions that are provided by Django are described in Aggregation Functions below. Since aggregates are also query expressions, you may combine aggregates with other aggregates or values to create complex aggregates.
Aggregates specified using keyword arguments will use the keyword as the name for the annotation. Anonymous arguments will have a name generated for them based upon the name of the aggregate function and the model field that is being aggregated. Complex aggregates cannot use anonymous arguments and must specify a keyword argument as an alias.
For example, when you are working with blog entries, you may want to know the number of authors that have contributed blog entries:
>>> from django.db.models import Count >>> q = Blog.objects.aggregate(Count('entry')) {'entry__count': 16}
By using a keyword argument to specify the aggregate function, you can control the name of the aggregation value that is returned:
>>> q = Blog.objects.aggregate(number_of_entries=Count('entry')) {'number_of_entries': 16}
For an in-depth discussion of aggregation, see the topic guide on Aggregation.
Please login to continue.