Opened 8 years ago

Closed 8 years ago

#27126 closed Cleanup/optimization (fixed)

Use `namedtuple` in result of `{% regroup %}`

Reported by: Baptiste Mispelon Owned by: nobody
Component: Template system Version: 1.10
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Tim Graham)

Currently, {% regroup %} returns a list of dictionaries with two key (grouper and list).

I propose changing that to return a namedtuple with the same fields.

This should be backwards-compatible because attribute access and key access inside template uses the same syntax.

The added benefit would be that you could now write:

{% regroup places by country as country_list %}

<ul>
{% for country, cities in country_list %}
    <li>{{ country }}
    <ul>
        {% for city in cities %}
          <li>{{ city.name }}: {{ city.population }}</li>
        {% endfor %}
    </ul>
    </li>
{% endfor %}
</ul>

Change History (3)

comment:2 by Tim Graham, 8 years ago

Description: modified (diff)
Triage Stage: UnreviewedAccepted

comment:3 by Tim Graham <timograham@…>, 8 years ago

Resolution: fixed
Status: newclosed

In 61b45dff:

Fixed #27126 -- Made {% regroup %} return a namedtuple to ease unpacking.

Note: See TracTickets for help on using tickets.
Back to Top