views:

571

answers:

2

I'm wanting to add a label= attribute to an option element of a Select form input using the Django Forms API without overwriting the Select widget's render_options method. Is this possible, if so, how?

Note: I'm wanting to add a label directly to the option (this is valid in the XHTML Strict standard) not an optgroup.

A: 

I'm afraid this isn't possible without subclassing the Select widget to provide your own rendering, as you've guessed. The code for Select doesn't include any attributes for each <option> item. It covers the option value, the "selected" status, and the label... that's all, I'm afraid:

def render_option(option_value, option_label):
    option_value = force_unicode(option_value)
    selected_html = (option_value in selected_choices) and u' selected="selected"' or ''
    return u'<option value="%s"%s>%s</option>' % (
        escape(option_value), selected_html,
        conditional_escape(force_unicode(option_label)))
Jarret Hardie
A: 

I just wrote a class to do that:

from django.forms.widgets import Select
from django.utils.encoding import force_unicode
from itertools import chain
from django.utils.html import escape, conditional_escape


class ExtendedSelect(Select):
    """
    A subclass of Select that adds the possibility to define additional 
    properties on options.

    It works as Select, except that the ``choices`` parameter takes a list of
    3 elements tuples containing ``(value, label, attrs)``, where ``attrs``
    is a dict containing the additional attributes of the option.
    """

    def render_options(self, choices, selected_choices):
        def render_option(option_value, option_label, attrs):
            option_value = force_unicode(option_value)
            selected_html = (option_value in selected_choices) and u' selected="selected"' or ''
            attrs_html = []
            for k, v in attrs.items():
                attrs_html.append('%s="%s"' % (k, escape(v)))
            if attrs_html:
                attrs_html = " " + " ".join(attrs_html)
            else:
                attrs_html = ""
            return u'<option value="%s"%s%s>%s</option>' % (
                escape(option_value), selected_html, attrs_html,
                conditional_escape(force_unicode(option_label)))
        # Normalize to strings.
        selected_choices = set([force_unicode(v) for v in selected_choices])
        output = []
        for option_value, option_label, option_attrs in chain(self.choices, choices):
            if isinstance(option_label, (list, tuple)):
                output.append(u'<optgroup label="%s">' % escape(force_unicode(option_value)))
                for option in option_label:
                    output.append(render_option(*option))
                output.append(u'</optgroup>')
            else:
                output.append(render_option(option_value, option_label,
                    option_attrs))
        return u'\n'.join(output)

Example:

select = ExtendedSelect(choices=(
        (1, "option 1", {"label": "label 1"}),
        (2, "option 2", {"label": "label 2"}),
    ))
Luper Rouch