From d302e2c09c281e6b1e187f704f656e5e79e69246 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Wed, 3 May 2017 07:21:44 -0400 Subject: Fixed #28157 -- Fixed choice ordering in form fields with grouped and non-grouped options. Regression in b52c73008a9d67e9ddbb841872dc15cdd3d6ee01. --- django/forms/widgets.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) (limited to 'django/forms') diff --git a/django/forms/widgets.py b/django/forms/widgets.py index 2feded85c7..37c11f1241 100644 --- a/django/forms/widgets.py +++ b/django/forms/widgets.py @@ -547,25 +547,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 = ( -- cgit v1.3