必需的布尔字段? [英] Required boolean field?
本文介绍了必需的布尔字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个布尔字段,
is_operable = BooleanField(widget = RadioSelect(choices = YES_OR_NO,renderer = InlineRadioFieldRenderer ),required = False,initial = True)
哪个被渲染为两个单选按钮(是或否),但我希望它是必需的。问题是,如果我将它更改为 required = True
,当它获得 False
时会抛出验证错误。 / p>
有没有办法?
YES_OR_NO =(
pre>
(True,'Yes'),
(False,'No')
)
解决方案我建议使用一个
TypedChoiceField
code> YES_OR_NO 转换为布尔值。请参阅文档:
http:/ /docs.djangoproject.com/en/1.2/ref/forms/fields/#django.forms.TypedChoiceFieldI've got a boolean field,
is_operable = BooleanField(widget=RadioSelect(choices=YES_OR_NO, renderer=InlineRadioFieldRenderer), required=False, initial=True)
Which is rendered as two radio buttons (yes or no), but I want it to be required. The problem is that if I change it to
required=True
, it throws a validation error when it getsFalse
.Is there a way around this?
YES_OR_NO = ( (True, 'Yes'), (False, 'No') )
解决方案I would recommend using a
TypedChoiceField
which coerces the choicesYES_OR_NO
into a boolean. See the docs: http://docs.djangoproject.com/en/1.2/ref/forms/fields/#django.forms.TypedChoiceField这篇关于必需的布尔字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文