Django's QuerySet
has two methods, annotate
and aggregate
. The documentation says that:
Unlike aggregate(), annotate() is not a terminal clause. The output of the annotate() clause is a QuerySet.
Is there any other difference between them? If not, then why does aggregate
exist?
I would focus on the example queries rather than your quote from the documentation.
Aggregate
calculates values for the entire queryset.Annotate
calculates summary values for each item in the queryset.Aggregation
Returns a dictionary containing the average price of all books in the queryset.
Annotation
q
is the queryset of books, but each book has been annotated with the number of authors.Aggregate Aggregate generate result (summary) values over an entire QuerySet. Aggregate operate over the rowset to get a single value from the rowset.(For example sum of all prices in the rowset). Aggregate is applied on entire QuerySet and it generate result (summary) values over an entire QuerySet.
In Model:
In Shell:
Annotate Annotate generate an independent summary for each object in a QuerySet.(We can say it iterate each object in a QuerySet and apply operation)
In Model:
In View:
In view it will count the likes for each video
That's the main difference, but aggregates also work on a grander scale than annotations. Annotations are inherently related to individual items in a queryset. If you run an
Count
annotation on a something like a many-to-many field, you'll get a separate count for each member of the queryset (as an added attribute). If you were to do the same with an aggregation, however, it would attempt to count every relationship on every member of the queryset, even duplicates, and return that as just one value.