开发者

Required boolean field?

开发者 https://www.devze.com 2023-01-10 02:05 出处:网络
I\'ve got a boolean field, is_operable = BooleanField(widget=RadioSelect(choices=YES_OR_NO, renderer=InlineRadioFieldRenderer), required=False, initial=True)

I'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 gets False.

Is there a way around this?


YES_OR_NO = (
    (True, 'Yes'),
    (False, 'No')
)


I would recommend using a TypedChoiceField which coerces the choices YES_OR_NO into a boolean. See the docs: http://docs.djangoproject.com/en/1.2/ref/forms/fields/#django.forms.TypedChoiceField


Leave it as required=False and write a custom clean_is_operable method that checks that you've got what you want, else throws a ValidationError or the like

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号