Select distinct users with referrals

Posted by Mark on Stack Overflow See other posts from Stack Overflow or by Mark
Published on 2010-05-20T22:20:47Z Indexed on 2010/05/20 23:00 UTC
Read the original article Hit count: 249

Filed under:
|
|
|

I have a bunch of Users. Since Django doesn't really let me extend the default User model, they each have Profiles. The Profiles have a referred_by field (a FK to User). I'm trying to get a list of Users with >= 1 referral. Here's what I've got so far

Profile.objects.filter(referred_by__isnull=False).values_list('referred_by', flat=True)

Which gives me a list of IDs of the users who have referrals... but I need it to be distinct, and I want the User object, not their ID.

Or better yet, it would be nice if it could return the number of referrals a user has.

Any ideas?

© Stack Overflow or respective owner

Related posts about django

Related posts about django-models