On Feb 6, 7:15 pm, Robert Lehmann <lehman...@gmail.com> wrote: > I'm trying to add additional data to an aggregated query, like so: > (there are many MyModels for any date, and I want to sum up > MyModel.value for every single date, and then do a calculation on that > sum) > > qs = MyModel.objects.values('date').annotate(count=Sum('value')) > for item in qs: > item['kilocount'] = item['count'] / 1000 > > Now, it sounds reasonable to do that directly in the database: > > MyModel.objects.values('date').annotate(count=Sum('value')).extra(select={'kilocount': > '"count" / 1000')) <SNIP> > Is there no way to do this through the ORM on the database? > (Another solution would probably be a custom template filter, but that > doesn't strike me as too idiomatic.)
I don't see any way to do that currently. The ORM has little hope to know this particular extra clause references an aggregate and thus must not be put into the GROUP BY clause. There is a ticket which would allow you to do this, but it will not get into 1.4 for sure, and we will have to see what is the position of core devs regarding adding more features to the ORM. It is pretty complex already. Anyways, the ticket is #14030 (https:// code.djangoproject.com/ticket/14030). I would just do that looping in code. It is simple and works. Someday the ORM might be able to do that in the ORM, but not yet. - Anssi -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.