summaryrefslogtreecommitdiff
path: root/django/forms
diff options
context:
space:
mode:
authorTim Graham <timograham@gmail.com>2017-05-03 07:21:44 -0400
committerTim Graham <timograham@gmail.com>2017-05-03 07:22:00 -0400
commit0615601498e6f7354e05498fc589c556f5365280 (patch)
treed30bdf649c9bea32c16cafc2943d66bade5a0c00 /django/forms
parent72a93da1b664f643fff39f105d19e7b5c980ddf5 (diff)
[1.11.x] Fixed #28157 -- Fixed choice ordering in form fields with grouped and non-grouped options.
Regression in b52c73008a9d67e9ddbb841872dc15cdd3d6ee01. Backport of d302e2c09c281e6b1e187f704f656e5e79e69246 from master
Diffstat (limited to 'django/forms')
-rw-r--r--django/forms/widgets.py14
1 files changed, 6 insertions, 8 deletions
diff --git a/django/forms/widgets.py b/django/forms/widgets.py
index a63297b5c4..e84db8d7c0 100644
--- a/django/forms/widgets.py
+++ b/django/forms/widgets.py
@@ -567,25 +567,23 @@ class ChoiceWidget(Widget):
def optgroups(self, name, value, attrs=None):
"""Return a list of optgroups for this widget."""
- default = (None, [], 0)
- groups = [default]
+ groups = []
has_selected = False
- for option_value, option_label in chain(self.choices):
+ for index, (option_value, option_label) in enumerate(chain(self.choices)):
if option_value is None:
option_value = ''
+ subgroup = []
if isinstance(option_label, (list, tuple)):
- index = groups[-1][2] + 1
+ group_name = option_value
subindex = 0
- subgroup = []
- groups.append((option_value, subgroup, index))
choices = option_label
else:
- index = len(default[1])
- subgroup = default[1]
+ group_name = None
subindex = None
choices = [(option_value, option_label)]
+ groups.append((group_name, subgroup, index))
for subvalue, sublabel in choices:
selected = (