[Django]-Django annotation in a loop

4👍

You can use dict unpacking with the ** operator to provide dynamic keyword arguments to any function and you should probably reassign the query:

query = query.annotate(**{'day_' + i + '_usage': "doesn't matter"})

1👍

When you do an annotation or other operation on a queryset, it returns a new instance of the queryset. It doesn’t modify the previous queryset.

So change your statement in the loop to

query = query.annotate(...)
👤Jason

Leave a comment