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).