Menu Close

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

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.

Posted in Python, Python Answers