Changes between Initial Version and Version 1 of CookBookTemplateTagRegroup


Ignore:
Timestamp:
Feb 27, 2006, 4:56:13 AM (18 years ago)
Author:
anonymous
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • CookBookTemplateTagRegroup

    v1 v1  
     1= CookBook - Template tag Regroup =
     2
     3If you need to group a list of objects by a choice like fied '''choices = CATEGORY_CHOICES''',
     4here is the trick.
     5
     6A Model.
     7{{{
     8#!python
     9
     10CATEGORY_CHOICES = (
     11        (1, 'Entrées'),
     12        (2, 'Poissons et crustacés'),
     13        (3, 'Viandes et volailles'),
     14        (4, 'Légumes et accompagnements'),
     15        (5, 'Desserts'),
     16        (6, 'Boissons'),
     17        (8, 'Autres'),
     18)       
     19
     20class Recette(meta.Model):
     21        [...]
     22        categorie = meta.SmallIntegerField(choices = CATEGORY_CHOICES)
     23
     24}}}
     25
     26The Rrgroup tag in action, the thing is that you don't want to display the category
     27number bit its corresponding name. (e.g not '1' but 'Entrées').
     28
     29Temmplate
     30{{{
     31#!python
     32
     33{% regroup recettes by categorie as grouped %}
     34<ul>
     35{% for group in grouped %}
     36        <li>{{ group.list.0.get_categorie_display }}
     37    <ul>
     38        {% for item in group.list %}
     39        <li>{{ item }}</li>
     40        {% endfor %}
     41    </ul>
     42{% endfor %}
     43</ul>
     44
     45}}}
Back to Top