【发布时间】:2020-06-10 05:48:12
【问题描述】:
我将几个forms.SelectMultiple 小部件作为呈现它们的快捷方式传递给视图。有没有办法通过默认检查哪些选项?源代码似乎不允许这样做:
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)
再次重申,我只是在使用小部件。它没有绑定到任何表单域,所以我不能使用initial。
【问题讨论】:
-
你还在用 Django-1.8 吗?
-
@WillemVanOnsem 不取决于我
标签: django django-1.8