django aggregation: sum then average

前端 未结 2 1126
北海茫月
北海茫月 2021-02-07 07:11

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

2条回答
  •  走了就别回头了
    2021-02-07 07:55

    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'))

提交回复
热议问题