More strict query yields more results?
Posted
by Mark
on Stack Overflow
See other posts from Stack Overflow
or by Mark
Published on 2010-05-28T02:17:56Z
Indexed on
2010/05/28
2:21 UTC
Read the original article
Hit count: 286
django
|django-query
I've got a query that looks like this
affiliates = User.objects.annotate(referral_count=Count('referrals')).filter(referral_count__gt=0)
But when I restrict it more by adding referrals__user__date_joined__gt=start_date
to the filter, I get even higher referral counts.
I'm trying to count the number of people a user has referred within a time period.
The relevant model looks like this:
class Profile(models.Model):
user = models.ForeignKey(User, unique=True, related_name='profile')
referred_by = models.ForeignKey(User, null=True, blank=True, related_name='referrals')
In contrast, when I try it like this:
affiliates = User.objects \
.exclude(referrals__user__date_joined__lte=sd, referrals__user__date_joined__gt=ed) \
.annotate(referral_count=Count('referrals')) \
.filter(referral_count__gt=0)
The exclude
statement seems to do nothing... the counts don't change (should be 0 if no referred users have joined within that time period, but it isn't).
© Stack Overflow or respective owner