title: Accessing distinct foreign key relationships with django queryset
body: <p>Given basic book/author models:</p>
  
  <pre><code>class Book(models.Model):
      title = models.CharField(max_length=200)
      author = models.ForeignKey('Author')
      publisher = models.ForeignKey('Publisher')
  
  class Author(models.Model):
      first_name = models.CharField(max_length=100)
      last_name = models.CharField(max_length=100)
  </code></pre>
  
  <p>Lets say I have a very complex query to select books based on a bunch of parameters (my actual models are much more complex than these).  For simplification sake, my complex query for this example will just select books that have the word "the" in their title:</p>
  
  <pre><code>q = Book.objects.filter(title__icontains="the")
  </code></pre>
  
  <p>Is there a way (other than looping, or using reverse foreign key lookups) to get all distinct <code>Author</code> objects linked to the books in this query?  E.g. I have tried adding:</p>
  
  <pre><code>q = q.values('author').distinct()
  </code></pre>
  
  <p>But this simply returns the <code>author__id values</code>. I am trying to go about it this way, as my 'complex query' is quite time/resource intensive, and I'd only like to run that query once (to get the books, and separate a list of distinct authors).  Also the data sets need to be flattened (a separate list of Books and Authors) as it is getting presented via django rest framework, and my clients require the data sets to be flat.  E.g.:</p>
  
  <pre><code>{
    "books": [
        {"id": 1, "title": "The Book.", "author": 1, "publisher": 1},
        {"id": 2, "title": "The Other Book.", "author": 2, "publisher": 1},
        {"id": 3, "title": "The Best Book.", "author": 1, "publisher": 1},
      ],
    "authors": [
        {"id": 1, "first_name": "Joe", "last_name": "Smith"},
        {"id": 2, "first_name": "Gina", "last_name": "Randolph"}
      ]
  }
  </code></pre>
  
  <p>Alternatively, is there a simple way with django rest framework to take the results of a complex query with nested foreign keys, and flatten it out?</p>