Opened 18 years ago

Closed 16 years ago

Last modified 13 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: dev
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@…> 18 years ago.
4412-2.diff (8.9 KB ) - added by Matt McClanahan <cardinal@…> 18 years ago.
4412-3.diff (9.0 KB ) - added by Matt McClanahan <cardinal@…> 17 years ago.
Updated for the unicode branch merge
4412-4.diff (9.0 KB ) - added by Matt McClanahan <cardinal@…> 17 years ago.
Bugfix, sigh.
4412.diff (8.9 KB ) - added by Tai Lee <real.human@…> 17 years ago.
updated to work on trunk, after unicode changes.
4412-r6025.diff (10.7 KB ) - added by Russell Keith-Magee 17 years ago.
Update to r6025, plus some polish to docs and implementation
4412-r7000.diff (7.2 KB ) - added by Tai Lee <real.human@…> 17 years ago.
Updated to r7000.
4412.2.diff (11.9 KB ) - added by Chris Beaven 16 years ago.
Updated patch, fixed to work with multiple select and old admin

Download all attachments as: .zip

Change History (26)

by Matt McClanahan <cardinal@…>, 18 years ago

Attachment: 4412-1.diff added

comment:1 by Matt McClanahan <cardinal@…>, 18 years ago

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 by James Bennett, 18 years ago

Triage Stage: UnreviewedDesign decision needed

comment:3 by Jacob, 18 years ago

Needs documentation: set
Triage Stage: Design decision neededAccepted

Accepted; needs documentation.

by Matt McClanahan <cardinal@…>, 18 years ago

Attachment: 4412-2.diff added

comment:4 by Matt McClanahan <cardinal@…>, 18 years ago

Needs documentation: unset

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

by Matt McClanahan <cardinal@…>, 17 years ago

Attachment: 4412-3.diff added

Updated for the unicode branch merge

by Matt McClanahan <cardinal@…>, 17 years ago

Attachment: 4412-4.diff added

Bugfix, sigh.

by Tai Lee <real.human@…>, 17 years ago

Attachment: 4412.diff added

updated to work on trunk, after unicode changes.

comment:5 by Simon G. <dev@…>, 17 years ago

Triage Stage: AcceptedReady for checkin

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

by Russell Keith-Magee, 17 years ago

Attachment: 4412-r6025.diff added

Update to r6025, plus some polish to docs and implementation

comment:6 by Matt McClanahan, 17 years ago

Owner: changed from nobody to Matt McClanahan
Status: newassigned

comment:7 by Matt McClanahan, 17 years ago

Owner: changed from Matt McClanahan to nobody
Status: assignednew

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

comment:8 by Malcolm Tredinnick, 17 years ago

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.

by Tai Lee <real.human@…>, 17 years ago

Attachment: 4412-r7000.diff added

Updated to r7000.

comment:9 by anonymous, 17 years ago

milestone: 1.0 beta

comment:10 by Chris Beaven, 17 years ago

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.

by Chris Beaven, 16 years ago

Attachment: 4412.2.diff added

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

comment:11 by Chris Beaven, 16 years ago

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 by Chris Beaven, 16 years ago

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

comment:13 by Russell Keith-Magee, 16 years ago

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 by Santiago Zarate, 16 years ago

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 by Santiago Zarate, 16 years ago

Resolution: fixed
Status: closedreopened

comment:16 by Karen Tracey, 16 years ago

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.

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

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 by Jacob, 13 years ago

milestone: 1.0 beta

Milestone 1.0 beta deleted

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