title: django sum for a queryset
body: <p>I need to do an aggregation on a django queryset for sets of data. </p>
  
  <p>I have a model that looks something like this:</p>
  
  <pre><code>class Model1(models.Model):
      ... some fields here ...
  
  class Model2(models.Model):
      model1 = models.ForeignKey(Model1)
      number = models.IntegerField()
      active = models.BooleanField()
      category = models.CharField(choices=...)
  </code></pre>
  
  <p>They work fine, in case there is a typo I missed in that demo code. I now want to display  model1s with their related model2s grouped by category. I am doing this using the regroup template tag (note I have a class method called model2s that returns all the same as model2_set.filter(active=True) ):</p>
  
  <pre><code>{% regroup m1.model2s by category as m2_list %}
  </code></pre>
  
  <p>Then I display them as normal with regroup. This stuff also works fine. Now I need to add a total summary to the top of each category group with the sum of number for that group. So if I had 2 model2s of the same category referencing 1 Model1 and one of those had number=1 and the other had number = 2, then I need to display 3. </p>
  
  <p>In reality there will be a lot of Model2s so this can be an expensive query. </p>
  
  <p>I've looked at the docs and see aggregate and annotate, but neither seems to let me do the sum based on the category. I would like to not have to do this for each category:</p>
  
  <pre><code>m2sum = m1.model2s.filter(category='something').aggregate(Sum('number'))['number__sum']
  </code></pre>
  
  <p>Is there a cleaner way to do this, hopefully one the works nicely with regroup?</p>