I have a model some thing like this
class Task(models.Model):
progress = models.PositiveIntegerField()
estimated_days = models.PositiveIntegerField()
Now I would like to do a calculation Sum(progress * estimated_days)
on the database level. Using Django Aggregation I can have the sum for each field but not the summation of multiplication of fields.
With Django 1.8 and above you can now pass an expression to your aggregate:
Constants are also available, and everything is combinable:
Do you have several options:
progress_X_estimated_days
and update it in save overwrited method. Then do aggregation through this new field.Overwriting:
Update: for Django >= 1.8 please follow the answer provided by @kmmbvnr
it's possible using Django ORM:
here's what you should do:
Note: if the two fields are of different types, say
integer
&float
, the type you want to return should be passed as the first parameter ofSum
It's a late answer, but I guess it'll help someone looking for the same.
The solution depends on Django version.
django < 1.8
django >= 1.8