Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you dynamically assign aliases in a django aggregate?

I'm trying out the django aggregates. I'm dynamically calling these aggregates and need to assign and alias them on the fly. I want the resulting alias to be the name of the original field.

For example:

from django.db import models as aggregator

field = 'person'
reducer = getattr(aggregator, 'Sum')

query_set.aggregate(field=reducer(field))

I'm expecting this:

{'person':12}

But getting this:

{'field':12}

Is there a way I can dynamically assign the alias in this case?

like image 482
Slamice Avatar asked Mar 03 '16 16:03

Slamice


Video Answer


1 Answers

You can use unpack syntax and pass a dictionary:

query_set.aggregate(**{field:reducer(field)})
like image 61
ilse2005 Avatar answered Sep 28 '22 16:09

ilse2005