title: Django hits the database for each filter() call
body: <p>I have some Django 1.3 code that looks up many model instances in a loop, ie.</p>
  
  <pre><code>my_set = myinstance.subitem_set.all()
  
  for value in values:
    existing = my_set.filter(attr_name=value)
    if len(existing) == 1:
       ...
  </code></pre>
  
  <p>This works, but profiling SQL queries shows that it hits the DB on each iteration. According to <a href="https://docs.djangoproject.com/en/1.3/ref/models/querysets/" rel="nofollow">https://docs.djangoproject.com/en/1.3/ref/models/querysets/</a> iterating over the related items should eagerly load them, so I tried calling:</p>
  
  <pre><code>list(my_set)
  </code></pre>
  
  <p>However, this doesn't help. It does do a query to load all the sub-items, but then it still does an individual query for each sub-item inside the loop. How do I get it to use the cached set and not hit the DB each time? The DB is PostgreSQL 8.4.</p>