Ticket #13237: chioces.multigroup.diff

File chioces.multigroup.diff, 1.7 KB (added by IsakovAN, 5 years ago)
  • forms/widgets.py

    diff -u -r django.orig/forms/widgets.py django/forms/widgets.py
    old new  
    414414            return u'<option value="%s"%s>%s</option>' % (
    415415                escape(option_value), selected_html,
    416416                conditional_escape(force_unicode(option_label)))
     417        def render_group(option_value, option_label):
     418            return u'<optgroup label="%s">%s</optgroup>' %(escape(force_unicode(option_value)),''.join(process_list(option_label)))
     419        def process_list(l):
     420            output = []
     421            for option_value, option_label in l:
     422                if isinstance(option_label, (list, tuple)):
     423                    output.append(render_group(option_value, option_label))
     424                else:
     425                    output.append(render_option(option_value, option_label))
     426            return output
    417427        # Normalize to strings.
    418428        selected_choices = set([force_unicode(v) for v in selected_choices])
    419         output = []
    420         for option_value, option_label in chain(self.choices, choices):
    421             if isinstance(option_label, (list, tuple)):
    422                 output.append(u'<optgroup label="%s">' % escape(force_unicode(option_value)))
    423                 for option in option_label:
    424                     output.append(render_option(*option))
    425                 output.append(u'</optgroup>')
    426             else:
    427                 output.append(render_option(option_value, option_label))
     429        output = process_list(chain(self.choices, choices))
    428430        return u'\n'.join(output)
    429431
    430432class NullBooleanSelect(Select):
Back to Top