title: Django Annotate Sum
body: <p>I'm trying to get a simple sum for a column with several rows in a queryset.  My direct question is (a) how do I set <code>get_queryset()</code> to include a sum of a column and (b) how do I access that element in a template? Following <a href="https://stackoverflow.com/questions/8616343/django-calculate-the-sum-of-the-column-values-through-query/8616400">this</a> question:  </p>
  
  <pre><code>#models.py
  class ItemPrice( models.Model ):
      price = models.DecimalField ( max_digits = 8, decimal_places=2 )
      ....
  </code></pre>
  
  <p>There are two answers provided - one using the <code>.aggregate()</code> method which I don't believe returns a queryset and <code>.annotate()</code> method which I believe appends an item to the queryset.</p>
  
  <p>So, I would have expected that the following would add another item to the object list in this view:</p>
  
  <pre><code>#views.py
  def get_queryset(self):
      # generate table and filter down to a subquery.
      queryset = ItemPrice.objects.filter(&lt;some_filter&gt;)
      # sum the price for each row in the subquery.
      queryset = queryset.annotate(totals=Sum('price'))
      return queryset
  </code></pre>
  
  <p>Then in the the Template, I would be able to iterate through the object list like this:</p>
  
  <pre><code>#template.html
  {% for item in object_list %}
      {{ item }}
  {% endfor %}
  </code></pre>
  
  <p>With the expectation that one of the items (the last item?) would be <code>price_sum</code> and that the balance could be accessed as <code>price_sum.price</code>.</p>
  
  <p>However, when i add the following to my template, I get the prices for each line item - no summation.  </p>
  
  <pre><code>{% for item in object_list %}
      {{ item.totals }}
  {% endfor %}
  </code></pre>
  
  <p>But, I can't access the item.  I don't know if the problem is the view modification of the <code>get_queryset()</code> or if it's in the template?</p>