﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
30518	Multiple Count annotation with filter doesn't work properly.	Roman Krejčík	nobody	"Lets have following models 

{{{
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))
)
}}}"	Bug	closed	Database layer (models, ORM)	dev	Normal	duplicate	annotate		Unreviewed	0	0	0	0	0	0
