Categories
Python Answers

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

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *