Instantiate object in dictionary comprehension in Django/Python -


i'm trying make function can dynamically builds django queryset. reason keeps giving nameerror... can see what's going wrong?

doesn't work:

from django.db.models import sum  sum_fields = ['subtotal', 'id'] subtotal = invoice.objects.filter(id__in=id_list).aggregate(**{field: sum(field) field in sum_fields}) 

the error given nameerror: global name 'sum' not defined. but... i'm importing before try dictionary comprehension.

this work:

from django.db.models import sum  sum_fields = ['subtotal', 'id'] subtotal = invoice.objects.filter(id__in=id_list).aggregate(**dict([(field, sum(field)) field in sum_fields])) 

the last version works , should do, want know what's wrong dictionary comprehension.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -