CookBook - Template tag Regroup
django version : 0.91
If you need to group a list of objects by a choice (eg, field has choices = CATEGORY_CHOICES), here is the trick.
The Model:
CATEGORY_CHOICES = ( (1, 'Entrées'), (2, 'Poissons et crustacés'), (3, 'Viandes et volailles'), (4, 'Légumes et accompagnements'), (5, 'Desserts'), (6, 'Boissons'), (8, 'Autres'), ) class Recette(meta.Model): [...] categorie = meta.SmallIntegerField(choices = CATEGORY_CHOICES)
And here is the regroup
tag in action. The thing is that you don't want to display the category
number, but its corresponding name. (e.g not '1' but 'Entrées').
The Template:
{% regroup recettes by categorie as grouped %} <ul> {% for group in grouped %} <li>{{ group.list.0.get_categorie_display }} <ul> {% for item in group.list %} <li>{{ item }}</li> {% endfor %} </ul> {% endfor %} </ul>
Last modified
19 years ago
Last modified on Mar 3, 2006, 1:29:03 AM
Note:
See TracWiki
for help on using the wiki.