On Thu, 2009-03-12 at 10:31 +0000, Andrew Turner wrote:
> Hi,
> 
> I've got a UserProfile model:-
> 
> class UserProfile(models.Model):
>     user = models.ForeignKey(User, unique=True)
>     ...
>     friends = models.ManyToManyField(User, related_name='friend', blank=True)
>     ...
> 
> I'm trying to do a query which will return all the users who call a
> given user a 'friend', i.e. if Bob and Jane have marked Dave as their
> friend, I want to return Bob and Jane as being 'fans' of Dave. Hope
> that makes sense.
> 
> I've tried various things, including:-
> 
> fans = UserProfile.objects.filter(friends__friend=user)
> fans = User.objects.filter(friend=user)
> 
> where user is the 'Dave' user object, but they are giving spurious results.

The second one is close. However, since "user" is a User object, you
need to be a little more careful in the filter comparison and filter
against the UserProfile.user attribute. Thus:

        User.objects.filter(friend__user=user)
        
Regards,
Malcolm



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to