itertools.groupby()perfect for that. I assume Tasku Categoryhave the attribute nameyou want to sort. I don't know the Django API, so you probably want to move the sort to a db request, and you will need to look at the attributes of the widget to figure out how to access the task objects, but here is the basic formula:
from itertools import groupby
tasks.sort(key=lambda task: (task.category.name, task.name))
for category, category_tasks in groupby(tasks, key=lambda task: task.category):
print '%s:' % category.name
for task in category_tasks:
print '* %s' % task.name
This should print a list, for example:
Breakfast foods:
* eggs
* spam
Dinner foods:
* spam
* spam
* spam
NTN
source
share