Code

Opened 6 years ago

Closed 6 years ago

Last modified 3 years ago

#7863 closed (worksforme)

Documentation fix: Stress that template tag "regroup" needs a sorted list

Reported by: jedie Owned by: nobody
Component: Documentation Version: master
Severity: Keywords:
Cc: django@… Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

regroup does only a right job, if the list is sorted with the key for the grouping?!?

Example:
Source data:

people = [
    {'first_name': 'George', 'last_name': 'Bush', 'gender': 'Male'},
    {'first_name': 'Pat', 'last_name': 'Smith2', 'gender': 'Unknown'},
    {'first_name': 'Condoleezza', 'last_name': 'Rice', 'gender': 'Female'},
    {'first_name': 'Bill', 'last_name': 'Clinton', 'gender': 'Male'},
    {'first_name': 'Pat', 'last_name': 'Smith', 'gender': 'Unknown'},
    {'first_name': 'Margaret', 'last_name': 'Thatcher', 'gender': 'Female'},
]

Template tag:

{% regroup people by gender as grouped %}

Result:

 u'grouped': [{'grouper': 'Male',
                     'list': [{'first_name': 'George',
                               'gender': 'Male',
                               'last_name': 'Bush'}]},
                    {'grouper': 'Unknown',
                     'list': [{'first_name': 'Pat',
                               'gender': 'Unknown',
                               'last_name': 'Smith2'}]},
                    {'grouper': 'Female',
                     'list': [{'first_name': 'Condoleezza',
                               'gender': 'Female',
                               'last_name': 'Rice'}]},
                    {'grouper': 'Male',
                     'list': [{'first_name': 'Bill',
                               'gender': 'Male',
                               'last_name': 'Clinton'}]},
                    {'grouper': 'Unknown',
                     'list': [{'first_name': 'Pat',
                               'gender': 'Unknown',
                               'last_name': 'Smith'}]},
                    {'grouper': 'Female',
                     'list': [{'first_name': 'Margaret',
                               'gender': 'Female',
                               'last_name': 'Thatcher'}]}]}

work a round, using sorted, e.g:

people = sorted(people, key=lambda x: x['gender'])

Attachments (3)

defaulttags_patch.txt (539 bytes) - added by jedie 6 years ago.
r8016-regroup-sorted.diff (777 bytes) - added by adamv 6 years ago.
Attempt at clarifying the regroup docs.
7863-take-3.diff (770 bytes) - added by mir 6 years ago.
patch to improve the documentation; replaces previous patches

Download all attachments as: .zip

Change History (11)

Changed 6 years ago by jedie

comment:1 Changed 6 years ago by jedie

  • Component changed from Template system to Documentation
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Summary changed from template tag "regroup" needs a sorted list... to More info on template tag "regroup" (needs a sorted list)

OK, now i see in the sourcecode ./django/template/defaulttags.py this:

    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 %}

IMHO this should be added to the docu: http://www.djangoproject.com/documentation/templates/#regroup

comment:2 Changed 6 years ago by anonymous

The docs already mention this: Note that {% regroup %} does not order its input! Our example relies on the fact that the people list was ordered by gender in the first place.

comment:3 Changed 6 years ago by jedie

Yes. But IMHO it should be made many clearer.

btw. regroup without a dict sort makes IMHO no sense, so it should made a dict sort by default!

Changed 6 years ago by adamv

Attempt at clarifying the regroup docs.

comment:4 Changed 6 years ago by mir

  • Has patch set
  • milestone set to 1.0 beta
  • Triage Stage changed from Unreviewed to Ready for checkin

Please, let's drop the first patch (defaulttags_patch.txt), which changes the behaviour of the tag. The current behaviour is intended, and I don't think that the core developers want to change it. But you're right about the docs.

While the current docs mention that you have to sort before using the tag, it does so only after a lengthy explanation with many examples. The sorting requirement should better go before the examples. I have amended your patch so that its style fits the other tag descriptions.

Since it's a kind of documentation buggie, I put this into the 1.0beta milestone.

Changed 6 years ago by mir

patch to improve the documentation; replaces previous patches

comment:5 Changed 6 years ago by mir

  • Summary changed from More info on template tag "regroup" (needs a sorted list) to Documentation fix: Stress that template tag "regroup" needs a sorted list

comment:6 Changed 6 years ago by mir

  • milestone changed from 1.0 beta to 1.0

Sorry, just learned that normal bugs go to the 1.0 milestone, not 1.0beta.

comment:7 Changed 6 years ago by ubernostrum

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

Since the docs already do mention this, I don't see a bug here; yes, the explanation comes after the example, but until you've seen the example of what regroup does it wouldn't make sense to explain why the initial ordering matters.

comment:8 Changed 3 years ago by jacob

  • milestone 1.0 deleted

Milestone 1.0 deleted

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.