Django annotate() multiple times causes wrong answers

前端 未结 3 1334
忘掉有多难
忘掉有多难 2021-01-01 10:50

Django has the great new annotate() function for querysets. However I can\'t get it to work properly for multiple annotations in a single queryset.

For example,

3条回答
  •  一向
    一向 (楼主)
    2021-01-01 11:10

    tour_list = Tour.objects.all().annotate(tour_count=Count('tourcomment',distinct=True) ).annotate(history_count=Count('history',distinct=True) )
    

    You have to add distinct=True to get the proper result else it will return the wrong answer.

提交回复
热议问题