diff options
| author | Chris Beaven <smileychris@gmail.com> | 2011-09-18 04:09:44 +0000 |
|---|---|---|
| committer | Chris Beaven <smileychris@gmail.com> | 2011-09-18 04:09:44 +0000 |
| commit | e63fa0ff832e4a6c18aa9f427b643b3445c5abb0 (patch) | |
| tree | 347b26d96b63ca393300df65878eab8866820d03 /django/forms | |
| parent | 1ec25c4fb7df0fdbc30bb7ae1e06e97451dec361 (diff) | |
Fixes #8103 -- Select widget should only allow for one selected option
git-svn-id: http://code.djangoproject.com/svn/django/trunk@16848 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/forms')
| -rw-r--r-- | django/forms/widgets.py | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/django/forms/widgets.py b/django/forms/widgets.py index 9b95c31c92..3797c163bd 100644 --- a/django/forms/widgets.py +++ b/django/forms/widgets.py @@ -499,6 +499,8 @@ class CheckboxInput(Widget): return bool(initial) != bool(data) class Select(Widget): + allow_multiple_selected = False + def __init__(self, attrs=None, choices=()): super(Select, self).__init__(attrs) # choices can be any iterable, but we may need to render this widget @@ -518,14 +520,20 @@ class Select(Widget): def render_option(self, selected_choices, option_value, option_label): option_value = force_unicode(option_value) - selected_html = (option_value in selected_choices) and u' selected="selected"' or '' + if option_value in selected_choices: + selected_html = u' selected="selected"' + if not self.allow_multiple_selected: + # Only allow for a single selection. + selected_choices.remove(option_value) + else: + selected_html = '' return u'<option value="%s"%s>%s</option>' % ( escape(option_value), selected_html, conditional_escape(force_unicode(option_label))) def render_options(self, choices, selected_choices): # Normalize to strings. - selected_choices = set([force_unicode(v) for v in selected_choices]) + selected_choices = set(force_unicode(v) for v in selected_choices) output = [] for option_value, option_label in chain(self.choices, choices): if isinstance(option_label, (list, tuple)): @@ -571,6 +579,8 @@ class NullBooleanSelect(Select): return initial != data class SelectMultiple(Select): + allow_multiple_selected = True + def render(self, name, value, attrs=None, choices=()): if value is None: value = [] final_attrs = self.build_attrs(attrs, name=name) |
