2👍
✅
You can order your table in your view first and then give a simple ordered list to the template.
first group them by session number
sessions = [{'count': 13, 'session': 1, 'instrument': u'Piano'}, {'count': 4, 'session': 1, 'instrument': u'Cello'}, {'count': 2, 'session': 1, 'instrument': u'Violin'}, {'count': 14, 'session': 2, 'instrument': u'Piano'}, {'count': 1, 'session': 4, 'instrument': u'Cello'}]
values = set(map(lambda x:x['session'], sessions))
grouped_sessions = [[y for y in sessions if y['session']==x] for x in values]
then you should have something like:
[[{'count': 13, 'session': 1, 'instrument': u'Piano'}, {'count': 4, 'session': 1, 'instrument': u'Cello'}, {'count': 2, 'session': 1, 'instrument': u'Violin'}],[{'count': 14, 'session': 2, 'instrument': u'Piano'}],[{'count': 1, 'session': 4, 'instrument': u'Cello'}]]
now in your template do a for-loop inside a for-loop like:
<table><thead>...</thead><tbody>
<tr>
{% for g in grouped_sessions %}
<td><table>
{% for s in g %}
<tr><td>{{s.instrument}} {{s.count}}</td></tr>
{% endfor %}
</table></td>
{% endfor %}
</tr>
This will create many nested tables but visually it does the work.
Hope it helps.
Source:stackexchange.com