Using django\'s ORM annotate() and/or aggregate(): I want to sum up based on one category field and then average over the category values per date. I tried to do it using two an
I haven't done a deep dive, but I suspect that when you use values()
without an annotation, sum_for_field
values are being merged with ones that share the same date
. I think you need to evaluate the annotation right after you use a values() clause. Maybe something like below will resolve your issue:
result = queryset1.values('date').annotate(avg_final=Avg('sum_for_field'))