title: Django: Query to get User.usernames via foreign key
body: <p>Consider the following Friendship model, where for every friendship made, there is a User who <em>sent</em> the friend request, and a User who <em>received</em> the friend request:</p>
  
  <pre><code>from django.contrib.auth.models import User
  
  class Friendship(models.Model):
      sender = models.ForeignKey(User)
      receiver = models.ForeignKey(User)
  </code></pre>
  
  <p>How would I construct a query that would give me all the <strong><em>usernames</em></strong> of the Users who are in a Friendship with, say, user123?</p>
  
  <p>Keep in mind, user123 could be the sender or the receiver of any friendship he is in.</p>
  
  <p>Also, I only want User.username values to be returned, not an entire User object.</p>
  
  <hr>
  
  <p>So far, I have this clunky half/solution (I'm using <code>list()</code> so that I can add both user123's <em>receiving friends</em> and user123's <em>sending friends</em>, to eventually produce a list of all friends). The problem is, I'm getting all the User objects, and I only want the usernames...and there's gotta be a better way to do this.</p>
  
  <pre><code>friends_a = list(Friendship.objects.filter(sender=user123).values('receiver'))
  friends_b = list(Friendship.objects.filter(receiver=user123).values('sender'))
  friends_a = [] if not friends_a else friends_a[0].values()
  friends_b = [] if not friends_b else friends_b[0].values()
  all_friends = friends_a + friends_b
  </code></pre>