I have a view that returns my sales summary grouped by sales_date e.g.
[{'sale_date':datetime.datetime(2010,10,5,0,0), 'salesum':2, 'item':1},
 {'sale_date':datetime.datetime(2010,10,5,0,0), 'salesum':10,'item':3},
 {'sale_date':datetime.datetime(2010,10,6,0,0), 'salesum':1, 'item':1}]
I have done the grouping inside the template, and combined it with html ul and li tags to give me a nice grouping effect based on the sale_date
My template grouping is based on this code:
{% regroup salecursor by sale_date|date:"j/m/Y" as salelist %}
and
{{ saleitem.grouper }
The result is:
05/10/2010
06/10/2010
How do u get a running total for each group i.e. group one should have a total of 12 and the second group a total of 1 and have something of this effect;
05/10/2010
item2 Name - 10
Total 12
06/10/2010
Item1 Name - 1
Total 1
Thanks Gath
What? Don't you know of the running_total filter? 
Just kidding there. There isn't one such but it is pretty easy to write one. This is a bit inefficient as it traverses the grouped list again to sum the salesum values.
# app_filters.py
@register.filter
def running_total(sales_list):
    return sum(d.get('salesum') for d in sales_list)
And you can use it thus in your template:
{% regroup salecursor by sale_date|date:"j/m/Y" as salelist %}
<ul>
{% for sales in salelist %}
    <li>{{ sales.grouper }}
    <ul>
        {% for item in sales.list %}
        <li>{{ item.item }} - {{ item.salesum }}</li>
        {% endfor %}
        <li>Total: <b>{{ sales.list|running_total }}</b></li>
    </ul>
    </li>
{% endfor %}
</ul>
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With