必需的布尔字段? [英] Required boolean field?

查看:119
本文介绍了必需的布尔字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个布尔字段,

  is_operable = BooleanField(widget = RadioSelect(choices = YES_OR_NO,renderer = InlineRadioFieldRenderer ),required = False,initial = True)

哪个被渲染为两个单选按钮(是或否),但我希望它是必需的。问题是,如果我将它更改为 required = True ,当它获得 False 时会抛出验证错误。 / p>

有没有办法?






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

pre>

解决方案

我建议使用一个 TypedChoiceField code> YES_OR_NO 转换为布尔值。请参阅文档:
http:/ /docs.djangoproject.com/en/1.2/ref/forms/fields/#django.forms.TypedChoiceField


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

这篇关于必需的布尔字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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