django choiceField与CheckboxSelectMultiple:默认情况下全部选中? [英] django choiceField with CheckboxSelectMultiple: all selected by default?

查看:275
本文介绍了django choiceField与CheckboxSelectMultiple:默认情况下全部选中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将ChoiceField与CheckboxSelectMultiple小部件一起使用。
是否可以将所有复选框默认呈现为选中状态?
谢谢!

I'm using a choiceField with the CheckboxSelectMultiple widget. Is it possible to render all checkboxes as checked by default? Thanks!

推荐答案

只需从字段的选择中设置初始值,就像这样:

Just set the initial values from the field's choices, like this:

MY_CHOICES = (
    ("some", "Some choice"),
    ("another", "Another choice"),
    ("best", "Best choice")
)

...

multiple_choice = forms.MultipleChoiceField(
    label=u"Select multiple", 
    choices=MY_CHOICES, 
    widget=forms.widgets.CheckboxSelectMultiple, 
    initial=(c[0] for c in MY_CHOICES)
)

这篇关于django choiceField与CheckboxSelectMultiple:默认情况下全部选中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆