Opened 10 years ago

Closed 8 years ago

Last modified 5 years ago

#4412 closed (fixed)

Allow Select and SelectMultiple widgets to render HTML optgroups from nested choices structures

Reported by: Tai Lee Owned by: Chris Beaven
Component: Forms Version: master
Severity: Keywords: newforms select widget nested choices optgroup
Cc: real.human@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

if the label of a choices iterable is a list or tuple, treat that option as an option group with the value as the label.

this shouldn't break any backwards compatibility and negates the need for a separate SelectGrouped widget (which has been previously rejected).

Attachments (8)

4412-1.diff (7.3 KB) - added by Matt McClanahan <cardinal@…> 10 years ago.
4412-2.diff (8.9 KB) - added by Matt McClanahan <cardinal@…> 10 years ago.
4412-3.diff (9.0 KB) - added by Matt McClanahan <cardinal@…> 9 years ago.
Updated for the unicode branch merge
4412-4.diff (9.0 KB) - added by Matt McClanahan <cardinal@…> 9 years ago.
Bugfix, sigh.
4412.diff (8.9 KB) - added by Tai Lee <real.human@…> 9 years ago.
updated to work on trunk, after unicode changes.
4412-r6025.diff (10.7 KB) - added by Russell Keith-Magee 9 years ago.
Update to r6025, plus some polish to docs and implementation
4412-r7000.diff (7.2 KB) - added by Tai Lee <real.human@…> 9 years ago.
Updated to r7000.
4412.2.diff (11.9 KB) - added by Chris Beaven 8 years ago.
Updated patch, fixed to work with multiple select and old admin

Download all attachments as: .zip

Change History (26)

Changed 10 years ago by Matt McClanahan <cardinal@…>

Attachment: 4412-1.diff added

comment:1 Changed 10 years ago by Matt McClanahan <cardinal@…>

Summary: [patch] changed newforms' Select widget to accept nested choices and display using the optgroup tag.Allow Select and SelectMultiple widgets to render HTML optgroups from nested choices structures

Initial implementation proposal. An example choices structure (from the tests in the patch):

choices = (
    ('outer1', 'Outer 1'),
    ('Group 1', (
            ('inner1', 'Inner 1'),
            ('inner2', 'Inner 2'),
        )
    )
)

yields the following HTML:

<select name="nestchoice">
<option value="outer1">Outer 1</option>
<optgroup label="Group 1">
<option value="inner1">Inner 1</option>
<option value="inner2">Inner 2</option>
</optgroup>
</select>

comment:2 Changed 10 years ago by James Bennett

Triage Stage: UnreviewedDesign decision needed

comment:3 Changed 10 years ago by Jacob

Needs documentation: set
Triage Stage: Design decision neededAccepted

Accepted; needs documentation.

Changed 10 years ago by Matt McClanahan <cardinal@…>

Attachment: 4412-2.diff added

comment:4 Changed 10 years ago by Matt McClanahan <cardinal@…>

Needs documentation: unset

That reminded me that I overlooked get_FIELD_display. Done and done.

Changed 9 years ago by Matt McClanahan <cardinal@…>

Attachment: 4412-3.diff added

Updated for the unicode branch merge

Changed 9 years ago by Matt McClanahan <cardinal@…>

Attachment: 4412-4.diff added

Bugfix, sigh.

Changed 9 years ago by Tai Lee <real.human@…>

Attachment: 4412.diff added

updated to work on trunk, after unicode changes.

comment:5 Changed 9 years ago by Simon G. <dev@…>

Triage Stage: AcceptedReady for checkin

Ready for checkin: JKM accepted, has tests, has docs, looks good.

Changed 9 years ago by Russell Keith-Magee

Attachment: 4412-r6025.diff added

Update to r6025, plus some polish to docs and implementation

comment:6 Changed 9 years ago by Matt McClanahan

Owner: changed from nobody to Matt McClanahan
Status: newassigned

comment:7 Changed 9 years ago by Matt McClanahan

Owner: changed from Matt McClanahan to nobody
Status: assignednew

Er. No, I don't own this. :P

comment:8 Changed 9 years ago by Malcolm Tredinnick

Triage Stage: Ready for checkinAccepted

I think "ready to checkin" is a little optimistic at this point. As Russell points out in the related django-dev thread, the patch will have problems with existing admin, so it needs to wait until after newforms-admin is merged.

Changed 9 years ago by Tai Lee <real.human@…>

Attachment: 4412-r7000.diff added

Updated to r7000.

comment:9 Changed 8 years ago by anonymous

milestone: 1.0 beta

comment:10 Changed 8 years ago by Chris Beaven

Owner: changed from nobody to Chris Beaven
Status: newassigned

Note: I've got an updated version of this patch running which fixes a few issues... If i haven't put it live in a week, bug me about it.

Changed 8 years ago by Chris Beaven

Attachment: 4412.2.diff added

Updated patch, fixed to work with multiple select and old admin

comment:11 Changed 8 years ago by Chris Beaven

This new patch allows this to continue with the old admin (even if it is going to be short-lived - i needed it for now) and also makes MultipleSelect work. I abstracted some of the widget rendering logic between Select and MultipleSelect which also meant making MultipleSelect a subclass of Select, which I always thought it should have been anyway...

Added tests for the new MultipleSelect functionality.

comment:12 Changed 8 years ago by Chris Beaven

Amusingly, I see that Russ had already done the SelectMultiple work. But I like my abstraction a bit better.

comment:13 Changed 8 years ago by Russell Keith-Magee

Resolution: fixed
Status: assignedclosed

(In [7977]) Fixed #4412 -- Added support for optgroups, both in the model when defining choices, and in the form field and widgets when the optgroups are displayed. Thanks to Matt McClanahan <cardinal@…>, Tai Lee <real.human@…> and SmileyChris for their contributions at various stages in the life of this ticket.

comment:14 Changed 8 years ago by Santiago Zarate

i found a small problem when using nested choice structures:

on models.py

class Item(models.Model):

#common object properties
name = models.CharField(max_length=60)
category = models.CharField("Item Category", max_length=20, choices=ITEM_TREE)

on admin.py

class ItemAdmin(admin.ModelAdmin):

list_display = ('name', 'category',)
list_filter = ('category',)

when i browse to: /admin/item/item/

i see the choices bot most of them are python's tuples

Filter
By Item Category

All
Outer 1
(('inner1', 'Inner 1'),
('inner2', 'Inner 2'))

(screenshot: http://imagenes.zarate.net.ve/varias/djangobug.png)

comment:15 Changed 8 years ago by Santiago Zarate

Resolution: fixed
Status: closedreopened

comment:16 Changed 8 years ago by Karen Tracey

Resolution: fixed
Status: reopenedclosed

Please do not reopen fixed tickets to report new problems. If there is a problem with new function added by this ticket, it should be reported in its own ticket. This also sounds vaguely familiar so do check to see if it's already been reported in an open ticket.

Also, please use preview before submitting your ticket and learn the basics of WikiFormatting so that your text will be readable.

comment:17 in reply to:  16 Changed 8 years ago by Ramiro Morales

Replying to kmtracey:

This also sounds vaguely familiar so do check to see if it's already been reported in an open ticket.

I couldn't find an pre-existing ticket about this so I've opened #9954.

comment:18 Changed 5 years ago by Jacob

milestone: 1.0 beta

Milestone 1.0 beta deleted

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