Categories
Python Answers

How to do Summation of Multiplication of two fields with Python Django Aggregation?

Spread the love

To do summation of multiplication of two fields with Python Django aggregation, we can call the aggergrate method.

For instance, we write

from django.db.models import F

Task.objects.aggregate(total=Sum(F('progress') * F('estimated_days')))['total']

to call aggregate to use Sum and F to sum up the progress multiplied by the estimated_days values.

And then we get the total value from the aggregation to return the value.

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 *