Question

I have a query akin to the following:

SELECT SUM(ISNULL(table.name)) FROM table

How does that SUM translate into a QuerySet in Django? (i.e. What operation xyz does it translate to, in something like MyModel.objects.xyz()?)

1
62
6/26/2011 12:01:44 AM

Accepted Answer

Update: The following incorporates the ISNULL aspect of the original query:

from django.db.models import Sum

ModelName.objects.filter(field_name__isnull=True).aggregate(Sum('field_name'))

You're looking for the Sum aggregation function, which works as follows:

ModelName.objects.aggregate(Sum('field_name'))

See: https://docs.djangoproject.com/en/dev/ref/models/querysets/#sum

123
5/1/2013 5:58:56 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon