{{{
class User(models.Model):
pass
class Subscription(models.Model):
user = models.ForeignKey(User, models.CASCADE)
valid_to = models.DateTimeField()
class SubscriptionToAuthor(models.Model):
user = models.ForeignKey(User, models.CASCADE)
valid_to = models.DateTimeField()
}}}
then annotate on only one related model works fine
{{{
# this is ok
User.objects.annotate(
subscribed_authors=Count('subscriptiontoauthor',
filter=Q(subscriptiontoauthor__valid_to__gt=now))
)
}}}
or
{{{
# this is ok
User.objects.annotate(
subscribed_newspapers=Count('subscription',
filter=Q(subscription__valid_to__gt=now))
)
}}}
but Count returns incorrect (too big) count numbers when both annotations
are used on single query set
{{{
# wrong result
User.objects.annotate(
subscribed_authors=Count('subscriptiontoauthor',
filter=Q(subscriptiontoauthor__valid_to__gt=now))
).annotate(
subscribed_newspapers=Count('subscription',
filter=Q(subscription__valid_to__gt=now))
)
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/30518>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* status: new => closed
* version: 2.2 => master
* resolution: => duplicate
Comment:
Duplicate of #10060 (see
[https://docs.djangoproject.com/en/stable/topics/db/aggregation
/#combining-multiple-aggregations documentation]).
--
Ticket URL: <https://code.djangoproject.com/ticket/30518#comment:1>