您的位置:首页 > 产品设计 > UI/UE

django: BooleanField, how to set the default value to true? - Stack Overflow

2012-04-07 11:59 1436 查看
django: BooleanField, how to set the default value to true? - Stack Overflow


down vote
acceptedIf you're just using a vanilla form (not a ModelForm), you can set a Field initial value (http://docs.djangoproject.com/en/dev/ref/forms/fields/#django.forms.Field.initial) like

class MyForm(forms.Form):
my_field = forms.BooleanField(initial=True)
If you're using a ModelForm, you can set a default value on the model field (http://docs.djangoproject.com/en/dev/ref/models/fields/#default), which will apply to the resulting ModelForm.

Finally, if you want to dynamically choose at runtime whether or not your field will be selected by default, you can use the initial parameter to the form when you initialize it:

form = MyForm(initial={'my_field':True})
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐