[Django] #29059: Widget optgroup is not grouping choices with no group

11 views
Skip to first unread message

Django

unread,
Jan 25, 2018, 6:40:09 AM1/25/18
to django-...@googlegroups.com
#29059: Widget optgroup is not grouping choices with no group
------------------------------------------------+------------------------
Reporter: Riccardo Di Virgilio | Owner: nobody
Type: Bug | Status: new
Component: Forms | Version: 2.0
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
------------------------------------------------+------------------------
I'm having problems writing template for custom controls (a bootstrap
button bar) using optgroup.
the problem is that optgroup is not grouping choices without groups, this
makes very hard to create several button bars that grouped togheter:


{{{
from django.forms.widgets import ChoiceWidget

w = ChoiceWidget(choices = [
("one", 1),
("two", 2), [
"italian", (
("uno", 1),
("due", 2)
)
]
])
}}}

now see how choices are grouped from optgroup:


{{{
import json

print(json.dumps(w.optgroups("name", "one"), indent = 4))
}}}


{{{
[
[
null,
[
{
"name": "name",
"value": "one",
"label": 1,
"selected": true,
"index": "0",
"attrs": {
"checked": true
},
"type": null,
"template_name": null
}
],
0
],
[
null,
[
{
"name": "name",
"value": "two",
"label": 2,
"selected": false,
"index": "1",
"attrs": {},
"type": null,
"template_name": null
}
],
1
],
[
"italian",
[
{
"name": "name",
"value": "uno",
"label": 1,
"selected": false,
"index": "2_0",
"attrs": {},
"type": null,
"template_name": null
},
{
"name": "name",
"value": "due",
"label": 2,
"selected": false,
"index": "2_1",
"attrs": {},
"type": null,
"template_name": null
}
],
2
]
]
}}}

this means that if choices are flats (no groups) you are getting a number
of groups that is the same of the number of choices, and in order to fix
this you need to use the grouping filters on the template in order to
regroup choices.

I think this is a bug, and flat choices should be grouped together as None
by optgroup method

thanks

--
Ticket URL: <https://code.djangoproject.com/ticket/29059>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jan 25, 2018, 11:33:36 AM1/25/18
to django-...@googlegroups.com
#29059: ChoiceWidget.optgroups() doesn't group choices without a group together
--------------------------------------+------------------------------------

Reporter: Riccardo Di Virgilio | Owner: nobody
Type: Bug | Status: new
Component: Forms | Version: 2.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Tim Graham):

* stage: Unreviewed => Accepted


Comment:

It might be fine -- can you offer a patch?

--
Ticket URL: <https://code.djangoproject.com/ticket/29059#comment:1>

Django

unread,
Jan 25, 2018, 3:20:09 PM1/25/18
to django-...@googlegroups.com
#29059: ChoiceWidget.optgroups() doesn't group choices without a group together
-------------------------------------+-------------------------------------
Reporter: Riccardo Di | Owner: Abhishek
Virgilio | Gautam
Type: Bug | Status: assigned
Component: Forms | Version: 2.0

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Abhishek Gautam):

* owner: nobody => Abhishek Gautam
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/29059#comment:2>

Django

unread,
Jan 29, 2018, 2:52:26 AM1/29/18
to django-...@googlegroups.com
#29059: ChoiceWidget.optgroups() doesn't group choices without a group together
-------------------------------------+-------------------------------------
Reporter: Riccardo Di | Owner: Abhishek
Virgilio | Gautam
Type: Bug | Status: assigned
Component: Forms | Version: 2.0

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Riccardo Di Virgilio):

Sure i'll do this in the next days.
Is it ok to provide just the function?

thanks!

--
Ticket URL: <https://code.djangoproject.com/ticket/29059#comment:3>

Django

unread,
Feb 2, 2018, 8:26:33 AM2/2/18
to django-...@googlegroups.com
#29059: ChoiceWidget.optgroups() doesn't group choices without a group together
-------------------------------------+-------------------------------------
Reporter: Riccardo Di | Owner: Abhishek
Virgilio | Gautam
Type: Bug | Status: assigned
Component: Forms | Version: 2.0

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Carlton Gibson):

* needs_better_patch: 0 => 1
* has_patch: 0 => 1


Comment:

The suggested fix in [https://github.com/django/django/pull/9621 PR#9621]
breaks choice ordering: it will group all ungrouped choices at whatever
index the first ungrouped happens to have. This isn't something we should
impose. (We may want to present ungrouped default/common choices at the
beginning, some groups, and then some ungrouped "other" choices at the
end, for one example.)

How we might preserve current behaviour whilst allowing grouping of
ungrouped items seems tricky at best. It looks to me as if a better
approach would be to recommend using a custom widget and overriding
`optgroups` in order to gather ungrouped choices if that is what's
required.

I put this example on the PR. It's just an untested sketch but it should
demonstrate the idea:


{{{
def optgroups(self, name, value, attrs=None):
"""Override optgroups to group ungrouped choices at end."""
ret = []
none_choices = []
max_index = 0
for group_name, choices, index in super().optgroups(name, value,
attrs):
if group_name is None:
none_choices.append(choices)
else:
max_index = max(max_index, index)
ret.append((group_name, choices, index))

ret.append((None, none_choices, max_index +1))
return ret
}}}

As such I'd probably be inclined to close this as Won't Fix.

(We could document the suggestion, but "override methods to customise
behaviour" is just OOP.)

--
Ticket URL: <https://code.djangoproject.com/ticket/29059#comment:4>

Django

unread,
Feb 16, 2018, 1:56:21 PM2/16/18
to django-...@googlegroups.com
#29059: ChoiceWidget.optgroups() doesn't group choices without a group together
-------------------------------------+-------------------------------------
Reporter: Riccardo Di | Owner: Abhishek
Virgilio | Gautam
Type: Bug | Status: closed
Component: Forms | Version: 2.0
Severity: Normal | Resolution: wontfix
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* status: assigned => closed
* resolution: => wontfix


--
Ticket URL: <https://code.djangoproject.com/ticket/29059#comment:5>

Reply all
Reply to author
Forward
0 new messages