Ticket #16149: select_with_disabled.diff

File select_with_disabled.diff, 1.7 KB (added by Jody McIntyre, 13 years ago)

WIP patch

  • django/forms/widgets.py

     
    503503        return bool(initial) != bool(data)
    504504
    505505class Select(Widget):
    506     def __init__(self, attrs=None, choices=()):
     506    def __init__(self, attrs=None, choices=(), disabled_choices=()):
    507507        super(Select, self).__init__(attrs)
    508508        # choices can be any iterable, but we may need to render this widget
    509509        # multiple times. Thus, collapse it into a list so it can be consumed
    510510        # more than once.
    511511        self.choices = list(choices)
     512        self.disabled_choices = frozenset(disabled_choices)
    512513
    513514    def render(self, name, value, attrs=None, choices=()):
    514515        if value is None: value = ''
     
    522523
    523524    def render_option(self, selected_choices, option_value, option_label):
    524525        option_value = force_unicode(option_value)
    525         selected_html = (option_value in selected_choices) and u' selected="selected"' or ''
    526         return u'<option value="%s"%s>%s</option>' % (
    527             escape(option_value), selected_html,
     526        if (option_value in selected_choices):
     527            selected_html = u' selected="selected"'
     528        else:
     529            selected_html = ''
     530        if (option_value in self.disabled_choices):
     531            disabled_html = u' disabled="disabled"'
     532        else:
     533            disabled_html = ''
     534        return u'<option value="%s"%s%s>%s</option>' % (
     535            escape(option_value), selected_html, disabled_html,
    528536            conditional_escape(force_unicode(option_label)))
    529537
    530538    def render_options(self, choices, selected_choices):
Back to Top