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?
You can use unpack
syntax and pass a dictionary:
query_set.aggregate(**{field:reducer(field)})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With