KingFish
KingFish

Reputation: 9183

Django: Model Get Related Object

I asked a similar question earlier today:
I have the following, table / class:

 Class UserFriend(model.Model):
      user = models.ForeignKey(User, related_name='friend_users' )
      friend = models.ForeignKey(User, related_name='friend_friends')
      active = models.BooleanField()

where User is the built-in auth_user class, I want to get only the friends, not the userfriends object where user is a specific user.

I'm looking to do the following SQL:

 select u.* from auth_user u, user_friend uf where u.user_id = 5 
    and u.id = uf.friend_id  
    and active=1 --- where user_id = the user being queried..

Upvotes: 0

Views: 122

Answers (1)

PauliusZ
PauliusZ

Reputation: 413

If user is the one you want friends for:

user.friend_users.filter(active=True).values_list('friend', flat=True)

Upvotes: 1

Related Questions