Reputation: 9512
I am passing several forms.SelectMultiple
widgets to a view as a shortcut to render them. Is there any way to pass which options need to be checked by default? The source code doesn't seem to allow that:
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)
output = [format_html('<select multiple="multiple"{}>', flatatt(final_attrs))]
options = self.render_options(choices, value)
if options:
output.append(options)
output.append('</select>')
return mark_safe('\n'.join(output))
def value_from_datadict(self, data, files, name):
if isinstance(data, (MultiValueDict, MergeDict)):
return data.getlist(name)
return data.get(name, None)
Again, let me repeat I am only using the widget. It is not bound to any form field, so I can't use initial
.
Upvotes: 2
Views: 1236
Reputation: 476574
The list of selected elements is in the value
. So you can make a widget with:
CHOICES = [
('1', 'red'),
('2', 'green'),
('3', 'blue'),
]
widget=forms.SelectMultiple()
widget.render('item-name', value=['1', '3'], choices=CHOICES)
In the source code we see that the render_options
is implemented as [GitHub]:
def render_options(self, choices, selected_choices): # Normalize to strings. selected_choices = set(force_text(v) for v in selected_choices) output = [] for option_value, option_label in chain(self.choices, choices): if isinstance(option_label, (list, tuple)): output.append(format_html('<optgroup label="{}">', force_text(option_value))) for option in option_label: output.append(self.render_option(selected_choices, *option)) output.append('</optgroup>') else: output.append(self.render_option(selected_choices, option_value, option_label)) return '\n'.join(output)
and in the render_option
method [GitHub]:
def render_option(self, selected_choices, option_value, option_label): if option_value is None: option_value = '' option_value = force_text(option_value) if option_value in selected_choices: selected_html = mark_safe(' selected="selected"') if not self.allow_multiple_selected: # Only allow for a single selection. selected_choices.remove(option_value) else: selected_html = '' return format_html('<option value="{}"{}>{}</option>', option_value, selected_html, force_text(option_label))
so it checks if the value is in the list of values you passed.
Upvotes: 2