SQL- GROUP BY... COUNT GROUP BY... SUM Django ORM annotate(), values(), django.db.models Count Sum order_by():
GROUP BY... COUNT:
from django.db.models import Count
result = Books.objects.values('author')
.order_by('author')
.annotate(count=Count('author'))
: author count:
author | count
------------|-------
OneAuthor | 5
OtherAuthor | 2
... | ...
GROUP BY... SUM:
from django.db.models import Sum
result = Books.objects.values('author')
.order_by('author')
.annotate(total_price=Sum('price'))
: author total_price:
author | total_price
------------|-------------
OneAuthor | 100.35
OtherAuthor | 50.00
... | ...