Menu Close

How to calculate the sum of the column values through query with Python Django?

Sometimes, we want to calculate the sum of the column values through query with Python Django.

In this article, we’ll look at how to calculate the sum of the column values through query with Python Django.

How to calculate the sum of the column values through query with Python Django?

To calculate the sum of the column values through query with Python Django, we can use the aggregate method.

For instance, we write

from django.db.models import Sum

ItemPrice.objects.aggregate(Sum('price'))

to call aggregate with the Sum('price') to create an aggregation result by summing up the price column value of each row.

Conclusion

To calculate the sum of the column values through query with Python Django, we can use the aggregate method.

Posted in Python, Python Answers