[Answer]-Running total with annotated querysets in Django

1👍

arr = []
for obj in list:
    if isinstance(obj, dict):
        arr.append(obj[var_name] if var_name in obj else 0)
    else:
        arr.append(getattr(obj, var_name) or 0)
return sum(arr)

The reason is that elements in annotated queryset is not model instance anymore, it’s dict.

Leave a comment