title: Django Count of Items in a Field
body: <p>models.py</p>
  
  <pre><code>class Event(models.Model):
      name = models.CharField(max_length=20, unique=True)
      distance = models.IntegerField()
      date = models.DateField()
  
  class Category(models.Model):
      name = models.CharField(max_length=20, unique=True)
      description = models.CharField(max_length=20, unique=True)
      isnew = models.BooleanField(default=False)
  
  class Result(models.Model):
      event = models.ForeignKey(Event)
      category = models.ForeignKey(Category)
      score = models.IntegerField()
  </code></pre>
  
  <p>I want to do a query to return a count of each unique Category in the Result table, for a given Event.</p>
  
  <p>What I'm doing now is something like:</p>
  
  <pre><code>results = Result.objects.filter(event=myevent)
  categorycountdict = {}
  for r in results:
      if r.category in categorycountdict:
          categorycountdict[r.category] += 1
      else:
          categorycountdict[r.category] = 1
  </code></pre>
  
  <p>Is there a better way, perhaps by query instead of python.</p>