#25861: Performing two conditional counts on foreign key fields
--------------------------------+--------------------------------------
Reporter: luccascorrea | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 1.8
Severity: Normal | Resolution:
Keywords: QuerySet.extra | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+--------------------------------------
Comment (by jarshwah):
Does this query work correctly?
{{{
from django.db.models import Count
from django.db.models.expressions import Case, When
likes = Q()
Article.objects.annotate(
likes=Count(
Case(When(articlelike__date__gte=F('pubdate'), then=1, else=0)),
distinct=True
),
views=Count(
Case(When(articleview__date__gte=F('pubdate'), then=1, else=0)),
distinct=True
)
)
}}}
I'm guessing this will result in duplicates also, but can you give it a
try anyway?
The ORM doesn't do aggregation across multiple reverse relations (or m2m
relations) very well. I'm not too certain about the reason unfortunately.
Anssi would know better than I, but I suspect it has something to do with
not creating subqueries.
--
Ticket URL: <https://code.djangoproject.com/ticket/25861#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" 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].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/070.718538ad9b95fffcac857fc7658e9039%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.