title: Django count related objects
body: <p>How can I count related objects in Django (in less than <em>N</em> queries, where <em>N</em> is number of object).</p>
  
  <p>To clarify, let's say I have tables <em>A</em> and <em>B</em>. Every <em>B</em> is connected to exactly one <em>A</em>. Approach I tried:</p>
  
  <pre><code>A.objects.select_related().filter(attr=val)
  A[i].B_set.count()
  </code></pre>
  
  <p>Of course, for every <em>A[i]</em> I want to find out number of <em>B</em> objects Django executes one query.</p>
  
  <p>So the question is - is there a way to optimize that?</p>