Reputation: 10765
I'm having some trouble with a related field to User in my UserProfile model.
I have this field in my UserProfile model:
friends = models.ManyToManyField(User, null=True)
When I call
User.objects.get(pk=234).get_profile().friends.all()
I get the set of friends as User objects
When I call
User.objects.get(pk=234).friends_set.all()
I get a list of UserProfile objects.
Is there a way (without changing the relationship to be with a UserProfile object) to get each side of the relationship returned as either User or UserProfile?
EDIT:
Sorry for the confusion i figured out what i was trying to do:
user = User.objects.get(pk=234)
User.objects.filter(userprofile__friends=user).all()
Upvotes: 1
Views: 1877
Reputation: 53990
There isn't just one relationship so there is more then just the two sides you are considering. A user has a relationship with a profile object (FK) and another with numerous user objects (M2M).
Upvotes: 1
Reputation: 65854
I believe that this is the way to select the UserProfile
objects which are friends with a given user:
UserProfile.objects.filter(friends__user = 234)
And here are the User
objects for the same set of users:
User.objects.filter(userprofile__friends__user = 234)
Upvotes: 1