Code

Opened 7 years ago

Closed 5 years ago

Last modified 3 years ago

#4412 closed (fixed)

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

Reported by: mrmachine Owned by: SmileyChris
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@…> 7 years ago.
4412-2.diff (8.9 KB) - added by Matt McClanahan <cardinal@…> 7 years ago.
4412-3.diff (9.0 KB) - added by Matt McClanahan <cardinal@…> 7 years ago.
Updated for the unicode branch merge
4412-4.diff (9.0 KB) - added by Matt McClanahan <cardinal@…> 7 years ago.
Bugfix, sigh.
4412.diff (8.9 KB) - added by Tai Lee <real.human@…> 7 years ago.
updated to work on trunk, after unicode changes.
4412-r6025.diff (10.7 KB) - added by russellm 7 years ago.
Update to r6025, plus some polish to docs and implementation
4412-r7000.diff (7.2 KB) - added by Tai Lee <real.human@…> 6 years ago.
Updated to r7000.
4412.2.diff (11.9 KB) - added by SmileyChris 6 years ago.
Updated patch, fixed to work with multiple select and old admin

Download all attachments as: .zip

Change History (26)

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

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

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Summary changed from [patch] changed newforms' Select widget to accept nested choices and display using the optgroup tag. to 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 7 years ago by ubernostrum

  • Triage Stage changed from Unreviewed to Design decision needed

comment:3 Changed 7 years ago by jacob

  • Needs documentation set
  • Triage Stage changed from Design decision needed to Accepted

Accepted; needs documentation.

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

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

  • Needs documentation unset

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

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

Updated for the unicode branch merge

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

Bugfix, sigh.

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

updated to work on trunk, after unicode changes.

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

  • Triage Stage changed from Accepted to Ready for checkin

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

Changed 7 years ago by russellm

Update to r6025, plus some polish to docs and implementation

comment:6 Changed 7 years ago by mattmcc

  • Owner changed from nobody to mattmcc
  • Status changed from new to assigned

comment:7 Changed 7 years ago by mattmcc

  • Owner changed from mattmcc to nobody
  • Status changed from assigned to new

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

comment:8 Changed 7 years ago by mtredinnick

  • Triage Stage changed from Ready for checkin to 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.

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

Updated to r7000.

comment:9 Changed 6 years ago by anonymous

  • milestone set to 1.0 beta

comment:10 Changed 6 years ago by SmileyChris

  • Owner changed from nobody to SmileyChris
  • Status changed from new to 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.

Changed 6 years ago by SmileyChris

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

comment:11 Changed 6 years ago by SmileyChris

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 6 years ago by SmileyChris

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

comment:13 Changed 6 years ago by russellm

  • Resolution set to fixed
  • Status changed from assigned to 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 Changed 5 years ago by SantiagoZarate

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 5 years ago by SantiagoZarate

  • Resolution fixed deleted
  • Status changed from closed to reopened

comment:16 follow-up: Changed 5 years ago by kmtracey

  • Resolution set to fixed
  • Status changed from reopened to 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.

comment:17 in reply to: ↑ 16 Changed 5 years ago by ramiro

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 3 years ago by jacob

  • milestone 1.0 beta deleted

Milestone 1.0 beta 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.