Hi Seti, As documented this will yield the wrong results in most cases until subqueries are used instead of JOINs[1].
Cheers, Simon [1] https://docs.djangoproject.com/en/1.9/topics/db/aggregation/#combining-multiple-aggregations Le dimanche 15 mai 2016 12:42:44 UTC-4, Seti Volkylany a écrit : > > For working models.Count() I am using distinct=True. It is right worked if > not models.Sum(). > > next code right worked. > > def get_queryset(self, request): > qs = super(AnswerAdmin, self).get_queryset(request) > qs = qs.annotate( > count_likes=models.Count('likes', distinct=True), > count_comments=models.Count('comments', distinct=True), > ) > return qs > > I am added new annotation with models.Sum() and found not correct results > > qs = qs.annotate( > count_likes=models.Count('likes', distinct=True), > count_comments=models.Count('comments', distinct=True), > scope=models.Sum( > models.Case( > models.When(likes__liked_it=True, then=1), > models.When(likes__liked_it=False, then=-1), > output_field=models.IntegerField() > ), > ), > ) > > Even if I added attribute distinct=True to models.Sum(), it still worked > not correct. > > How made worked models.Sum() and models.Count() together? > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/77d11ccb-ce5f-4247-8640-474f0bdfe008%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.

