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
Post a Comment