78
Update: for Django >= 1.8 please follow the answer provided by @kmmbvnr
it’s possible using Django ORM:
here’s what you should do:
from django.db.models import Sum
total = ( Task.objects
.filter(your-filter-here)
.aggregate(
total=Sum('progress', field="progress*estimated_days")
)['total']
)
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 of Sum
It’s a late answer, but I guess it’ll help someone looking for the same.
104
With Django 1.8 and above you can now pass an expression to your aggregate:
from django.db.models import F
Task.objects.aggregate(total=Sum(F('progress') * F('estimated_days')))['total']
Constants are also available, and everything is combinable:
from django.db.models import Value
Task.objects.aggregate(total=Sum('progress') / Value(10))['total']
- [Django]-How do you filter a nested serializer in Django Rest Framework?
- [Django]-Celery discover tasks in files with other filenames
- [Django]-How can I set a default value for a field in a Django model?
37
The solution depends on Django version.
-
django < 1.8
from django.db.models import Sum MyModel.objects.filter(<filters>).aggregate(Sum('field1', field="field1*field2"))
-
django >= 1.8
from django.db.models import Sum, F MyModel.objects.filter(<filters>).aggregate(Sum(F('field1')*F('field2')))
- [Django]-Check if celery beat is up and running
- [Django]-How can I use Bootstrap with Django?
- [Django]-Why doesn't django's model.save() call full_clean()?
2
Edited (after Django 1.8)
Since Django 1.8, you can use F
:
from django.db.models import F. Sum
total = (
Task
.objects
.aggregate(total=Sum(F('progress') * F('estimated_days')))
['total']
)
Old answer (before Django 1.8)
This answer was wrote on 2012 and django 1.8 was published on 2015
Do you have several options:
- Raw query
- Emulbreh’s undocumented approach
- Create a third field
progress_X_estimated_days
and update it in save overwrited method. Then do aggregation through this new field.
Overwriting:
class Task(models.Model):
progress = models.PositiveIntegerField()
estimated_days = models.PositiveIntegerField()
progress_X_estimated_days = models.PositiveIntegerField(editable=False)
def save(self, *args, **kwargs):
progress_X_estimated_days = self.progress * self.estimated_days
super(Task, self).save(*args, **kwargs)
- [Django]-Django celery task: Newly created model DoesNotExist
- [Django]-Jquery template tags conflict with Django template!
- [Django]-Change model class name in Django admin interface
Source:stackexchange.com