title: Quick counting with linked Django Models
body: <p>I've got a stack of Models something like this (I'm typing it out in relative shorthand):</p>
  
  <pre><code>class User:
      pass
  
  
  class ItemList:
      pass
  
  
  # A User can have more than one ItemList
  # An ItemList can have more than one User
  # Classic M2M
  
  class ItemListOwnership:
      user = fk(User)
      itemlist = fk(ItemList)
  
  
  # An ItemList has multiple Items
  
  class Item:
      itemlist = fk(ItemList)
  </code></pre>
  
  <p>What I want to do is display a simple count of how many items a given user (or existing queryset of users) have on their various lists. <strong>I need to count across the M2M divide.</strong></p>
  
  <p>That's where I'm stuck. I'm happy with a pure SQL method if that's the only thing that works but I really don't want to get into a situation where I'm issuing <em>n + 1</em> queries per user (where n is the number of lists a user has) just for a count...</p>
  
  <p>Edit: I'm not using a "real" <code>models.ManyToMany(..)</code> relationship because I define additional things in <code>ItemListOwnership</code> that describe the relationship slightly better. If it's a killer, I can probably move this metadata elsewhere and get rid of <code>ItemListOwnership</code> and stick a m2m in <code>ItemList</code></p>