Answers for "django annotate vs aggregate"

0

django annotate vs aggregate

* django annotate vs aggregate
* Difference between Django's annotate and aggregate methods?
---------------------------------
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
>>> Book.objects.aggregate(average_price=Avg('price'))
{'average_price': 34.35}

Annotation
>>> q = Book.objects.annotate(num_authors=Count('authors'))
>>> q[0].num_authors
2
>>> q[1].num_authors
1
Posted by: Guest on April-08-2022

Python Answers by Framework

Browse Popular Code Answers by Language