I am processing a queryset in a view to get a summary of data and need to be able to iterate over the resultant dataset in my template. I did this OK with a key and single value by building a dictionary and then using .items to work over key value pairs using:
{% for id,total in list_summary.items %}
<tr class="{% cycle 'row1' 'row2' %}" >
<td>{{id}}</td>
<td>{{total}}</td>
</tr>
{% endfor %}
The problem I have is that I need more than just a key and value. I need multiple values. I tried building a dictionary using a dictionary as the value but in the template I can’t get to the values in the dictionary.
What is the appropriate datastructure to build to make this work? I thought of a temporary model or making a custom template tag to access the sub dictionary values but I think there must be a better way to process queryset data prior to feeding it to a view.
My view code is here:
lists=lists.objects.filter(user_id=user_id)
list_summary={}
# make a dictionary that we wil iterate over in template. The dictionary has the total spend for each deal
for list in lists:
id = list.id
name = list.deal_name
price = list.normal_price
quantity = list.quantity
total = price * quantity
try:
list_item=list_summary[id]
old_total=list_item['total']
list_summary[id]={'name':name, 'total':old_total + total}
except:
list_summary[id]={'name':name, 'total':total}
context = {
'list_summary':list_summary,
}
return render_to_response("lists.html", context)
Any help would be much appreciated!
Many thanks in advance
Rich
Why not? The values are also key/value pairs..