#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)
Change History (26)
by , 18 years ago
| Attachment: | 4412-1.diff added | 
|---|
comment:1 by , 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 | 
|---|
comment:2 by , 18 years ago
| Triage Stage: | Unreviewed → Design decision needed | 
|---|
comment:3 by , 18 years ago
| Needs documentation: | set | 
|---|---|
| Triage Stage: | Design decision needed → Accepted | 
Accepted; needs documentation.
by , 18 years ago
| Attachment: | 4412-2.diff added | 
|---|
comment:4 by , 18 years ago
| Needs documentation: | unset | 
|---|
That reminded me that I overlooked get_FIELD_display.  Done and done.
comment:5 by , 18 years ago
| Triage Stage: | Accepted → Ready for checkin | 
|---|
Ready for checkin: JKM accepted, has tests, has docs, looks good.
by , 18 years ago
| Attachment: | 4412-r6025.diff added | 
|---|
Update to r6025, plus some polish to docs and implementation
comment:6 by , 18 years ago
| Owner: | changed from to | 
|---|---|
| Status: | new → assigned | 
comment:7 by , 18 years ago
| Owner: | changed from to | 
|---|---|
| Status: | assigned → new | 
Er.  No, I don't own this. :P
comment:8 by , 18 years ago
| Triage Stage: | Ready for checkin → Accepted | 
|---|
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.
comment:9 by , 17 years ago
| milestone: | → 1.0 beta | 
|---|
comment:10 by , 17 years ago
| Owner: | changed from to | 
|---|---|
| Status: | new → assigned | 
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 , 17 years ago
| Attachment: | 4412.2.diff added | 
|---|
Updated patch, fixed to work with multiple select and old admin
comment:11 by , 17 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 , 17 years ago
Amusingly, I see that Russ had already done the SelectMultiple work. But I like my abstraction a bit better.
comment:13 by , 17 years ago
| Resolution: | → fixed | 
|---|---|
| Status: | assigned → closed | 
(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 , 17 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 , 17 years ago
| Resolution: | fixed | 
|---|---|
| Status: | closed → reopened | 
follow-up: 17 comment:16 by , 17 years ago
| Resolution: | → fixed | 
|---|---|
| Status: | reopened → closed | 
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.
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: