title: Joining ManyToMany fields with prefetch_related in Django
body: <p>I may be missing something obvious, but I'm having trouble getting a join on a ManyToMany field to work in a Django app. I have two models:</p>
  
  <pre><code>class Area(models.Model):
      name = CharField(...)
  
  class Role(models.Model):
      name = CharField(...)
      areas = ManyToManyField('Area', ...)
  </code></pre>
  
  <p>My goal is to have the equivalent of this query:</p>
  
  <pre><code>select a.name, r.name
  from area a
  join area_role ar on ar.area_id = a.id
  join role r on ar.role_id = r.id
  order by a.name, r.name
  </code></pre>
  
  <p>The resulting data set would look like:</p>
  
  <pre><code>Area    Role
  ---------------------
  A       My Role
  A       Your Role
  A       Yo Mamas Role
  B       My Role
  B       Some Other Role
  </code></pre>
  
  <p>As you can see in the example, the <strong>My Role</strong> item appears twice, once for each area. I know I could get the list of areas and then get a list of roles for each (resulting in N+1 queries), but I'd like to be efficient about it if possible. As such, I found that <a href="https://docs.djangoproject.com/en/1.8/ref/models/querysets/#prefetch-related" rel="noreferrer">prefetch_related</a> might be what I wanted to use. When I use this, however, I end up with all Area values as <strong>None</strong>. Here's what I tried:</p>
  
  <pre><code>rqs = ( Role.objects.filter(areas__id__in=[1,2,3])
          .prefetch_related(areas).order_by('areas__name', 'name') )
  
  for r in rqs:
      print("Area Name: " + str(r.areas.name))
      print("Role Name: " + str(r.name))
  </code></pre>
  
  <p>The role names come along for the ride correctly, but the area names do not. What am I doing wrong here?</p>