Sunday, May 15, 2016

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

My new method for resolve problem not working to:
(setattr(i, 'scope', i.get_scope()) for i in qs)
Django not found field 'scope'. (error from traceback)
----------------------------------
Cannot resolve keyword 'scope' into field. Choices are: answers, author, author_id, count_answers, count_opinions, count_tags, date_added, date_modified, id, is_dublicated, opinions, slug, status, tags, text_question, title, views
----------------------------------

понедельник, 16 мая 2016 г., 2:45:42 UTC+3 пользователь Simon Charette написал:
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 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/b2e09a2f-cc2b-4f42-9044-931de77b2e20%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment