title: Django: Most Efficient Way To Filter A Queryset Repeatedly
body: <p>I have a model that looks something like this:</p>
  
  <pre><code>class Item(models.Model):
      name = models.CharField()
      type = models.CharField()
      tags = models.models.ManyToManyField(Tags)
  </code></pre>
  
  <p>In order to render a given view, I have a view that presents a list of Items based on type.  So in my view, there's a query like:</p>
  
  <pre><code>items = Item.objects.filter(type='type_a')
  </code></pre>
  
  <p>So that's easy and straight forward.  Now I have an additional requirement for the view.  In order to fulfill that requirement, I need to build a dictionary that relates Tags to Items.  So the output i am looking for would be something like:</p>
  
  <pre><code>{
      'tag1': [item1, item2, item5],
      'tag2': [item1, item4],
      'tag3': [item3, item5]
  }
  </code></pre>
  
  <p>What would be the most efficient way to do this?  Is there any way to do this without going to the database with a new query for each tag?</p>