[Django] #27126: Use `namedtuple` in result of `{% regroup %}`

41 views
Skip to first unread message

Django

unread,
Aug 26, 2016, 5:49:51 AM8/26/16
to django-...@googlegroups.com
#27126: Use `namedtuple` in result of `{% regroup %}`
------------------------------------------------+------------------------
Reporter: bmispelon | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Template system | Version: 1.10
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 1
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
------------------------------------------------+------------------------
Currently, `{% regroup %}` returns a list of dictionaries with two key
(`grouper` and `list`).

I propose chaning 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:
{{{#!django
{% 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>

}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/27126>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Aug 26, 2016, 5:52:33 AM8/26/16
to django-...@googlegroups.com
#27126: Use `namedtuple` in result of `{% regroup %}`
-------------------------------------+-------------------------------------
Reporter: bmispelon | Owner: nobody
Type: | Status: new
Cleanup/optimization |

Component: Template system | Version: 1.10
Severity: Normal | Resolution:

Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by bmispelon):

PR here: https://github.com/django/django/pull/7157

--
Ticket URL: <https://code.djangoproject.com/ticket/27126#comment:1>

Django

unread,
Aug 26, 2016, 11:09:59 AM8/26/16
to django-...@googlegroups.com
#27126: Use `namedtuple` in result of `{% regroup %}`
--------------------------------------+------------------------------------

Reporter: bmispelon | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Template system | Version: 1.10
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by timgraham):

* stage: Unreviewed => Accepted


Old description:

> Currently, `{% regroup %}` returns a list of dictionaries with two key
> (`grouper` and `list`).
>
> I propose chaning 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:
> {{{#!django
> {% 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>
>
> }}}

New description:

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:
{{{#!django
{% 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>

}}}

--

--
Ticket URL: <https://code.djangoproject.com/ticket/27126#comment:2>

Django

unread,
Aug 26, 2016, 5:00:55 PM8/26/16
to django-...@googlegroups.com
#27126: Use `namedtuple` in result of `{% regroup %}`
--------------------------------------+------------------------------------
Reporter: bmispelon | Owner: nobody
Type: Cleanup/optimization | Status: closed

Component: Template system | Version: 1.10
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: new => closed
* resolution: => fixed


Comment:

In [changeset:"61b45dff6be3a2c85adac73acf5fe0de71f9ecda" 61b45dff]:
{{{
#!CommitTicketReference repository=""
revision="61b45dff6be3a2c85adac73acf5fe0de71f9ecda"
Fixed #27126 -- Made {% regroup %} return a namedtuple to ease unpacking.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/27126#comment:3>

Reply all
Reply to author
Forward
0 new messages