Opened 9 years ago

Closed 9 years ago

#1575 closed defect (invalid)

REGROUP tag makes two groups with the same value

Reported by: ronanpaixao@… Owned by: adrian
Component: Template system Version: 0.91
Severity: normal Keywords: regroup tag
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

I'm using a generic view (haven't tested with a custom view) and sqlite as backend.
The problem is that the regroup tag isn't working properly, as it is creating two groups with the same grouper value.
When I create the items in a table and use the grouper tag, it groups in the order inserted in the table, unless I specifically use an extra_lookup_kwargs.order_by option.
I think I'll explain better with an example:
I create a table with the columns Col1 and Col2
I create, in the following order the items:
-Val1, 1
-Val2, 1
-Val3, 2
-Val4, 1
Then I pass this table a generic view and use the following in the template:
{% regroup object_list by Col2 as grouped %}
{% for group in grouped %}
<br>Grouper: {{group.grouper}}

{% for equipe in group.list %}

  • {{equipe.nome}}<br>

{% endfor %}

{% endfor %}

It returns me the following:
Grouper: 1

  • Val1
  • Val2

Grouper: 2

  • Val3

Grouper: 1

  • Val4

Change History (3)

comment:1 Changed 9 years ago by anonymous

Sorry, I didn't know the HTML was used and didn't see the Preview button. Here's the post with the correct formatting:
I'm using a generic view (haven't tested with a custom view) and sqlite as backend.
The problem is that the regroup tag isn't working properly, as it is creating two groups with the same grouper value.
When I create the items in a table and use the grouper tag, it groups in the order inserted in the table, unless I specifically use an extra_lookup_kwargs.order_by option.
I think I'll explain better with an example:
I create a table with the columns Col1 and Col2
Then I create, in the following order the items:

-Val1, 1
-Val2, 1
-Val3, 2
-Val4, 1

Then I pass this table to a generic view and use the following in the template:

{% regroup object_list by Col2 as grouped %}
{% for group in grouped %}
<br>Grouper: {{group.grouper}}
	{% for equipe in group.list %}
			- {{equipe.nome}}<br>
	{% endfor %}
{% endfor %}

It returns me the following:

Grouper: 1
- Val1
- Val2
Grouper: 2
- Val3
Grouper: 1
- Val4

comment:2 Changed 9 years ago by akaihola

I think it works just as intended. From Django documentation:

Note that {% regroup %} does not work when the list to be grouped is not sorted by the key you are grouping by! This means that if your list of people was not sorted by gender, you'd need to make sure it is sorted before using it, i.e.:

{% regroup people|dictsort:"gender" by gender as grouped %}

comment:3 Changed 9 years ago by jacob

  • Resolution set to invalid
  • Status changed from new to closed

Oh, {% regroup %}, how do I love thee? Let me count the ways...

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