Sunday, May 15, 2016

How made worked models.Sum() together with models.Count in Django annotations?

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 django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
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/d39becab-80e3-44ad-a4ca-77233ac683f2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment