Answers for "sum query in django"

0

summation django queryset

from django.db.models import Sum

ItemPrice.objects.aggregate(Sum('price'))
# returns {'price__sum': 1000} for example
Posted by: Guest on June-19-2021
4

django group by

# If you mean to do aggregation you can use the aggregation features of the ORM:
from django.db.models import Count
Members.objects.values('designation').annotate(dcount=Count('designation'))

# This results in a query similar to:
SELECT designation, COUNT(designation) AS dcount
FROM members GROUP BY designation

#and the output would be of the form
[{'designation': 'Salesman', 'dcount': 2}, 
 {'designation': 'Manager', 'dcount': 2}]
Posted by: Guest on June-03-2020

Python Answers by Framework

Browse Popular Code Answers by Language