Search code examples
djangodjango-formsdjango-widget

SelectMultiple and OptionGroup in Django


I'm using option group-style choices for a django form field, like this:

MEDIA_CHOICES = (
    ('Audio', (
            ('vinyl', 'Vinyl'),
            ('cd', 'CD'),
        )
    ),
    ('Video', (
            ('vhs', 'VHS Tape'),
            ('dvd', 'DVD'),
        )
    ),
    ('unknown', 'Unknown'),
)

This works fine so long as the widget I am using is the default forms.widgets.Select. However, when I try to use forms.widget.SelectMultiple I get the following error message:

Select a valid choice. [u'vhs', u'dvd'] is not one of the available choices.

Solution

  • Changing the widget doesn't change the type of data accepted by a field. A normal ChoiceField still only expects a single value. If you want to accept more than one value, you need to use MultipleChoiceField.