django ModelForm似乎不验证BooleanField [英] django ModelForm doesn't seem to validate BooleanField
问题描述
我正在使用Django 1.5.4
I'm using django 1.5.4
这是我所面临问题的一个最小示例。
Here's a minimal example of the issue I'm facing.
模型:
#models.py
from django.db import models
class SampleModel(models.Model):
spam = models.BooleanField()
形式:
#forms.py
from django.forms import ModelForm
from .models import SampleModel
class SampleModelForm(ModelForm):
class Meta:
model = SampleModel
fields = ('spam', )
从Django shell:
From the django shell:
>>> data = {} #intentionally blank
>>> form = SampleModelForm(data)
>>> is_valid = form.is_valid() #is_valid is True
>>> form.save() # model instance is created with "spam" set to False by default.
我是否正确验证了表单? form.is_valid
正确验证其他类型的字段。
docs 表示默认情况下所有字段均为必填但是 is_valid
会返回 True
而没有布尔字段键。
Am I validating the form incorrectly? form.is_valid
validates fields of other types correctly.
The docs indicate that all fields are required by default but is_valid
returns True
without the boolean field key being present.
我需要确保输入数据中存在布尔字段。
到目前为止,我正在手动检查该字段是否存在并且类型为 bool
。您是否认为我应该覆盖 form.is_valid
并添加此检查,以便也可以将其重新用于其他模型?
I need to ensure that the boolean field is present in the input data.
As of now, I'm manually checking if the field is present and is of type bool
. Do you think I should override form.is_valid
and add this check so that it can be reused for other models too?
推荐答案
(根据代码检查;文档未说)证明模型 BooleanField
s具有 blank = True
在其 __ init __
方法中自动设置,因此不需要自动创建的模型表单字段。考虑到这是有道理的(False算作空白,因此 BooleanField
要求它为真),但是当您阅读文档时并不清楚。
It turns out (from code inspection; the docs don't say) that model BooleanField
s have blank=True
set automatically in their __init__
method, thus making the automatically created model form field not required. This makes sense upon consideration (False counts as blank, so BooleanField
s need it to be true) but it's not obvious when you just read the docs.
如果希望将其要求为 True
,则通常使用表单字段替代-自行声明该字段或设置其必需的
属性在验证之前为某个地方的 True
(我通常使用表单的 __ init __
方法)。如果要允许它允许 True
或 False
,但不允许Python None $ c $
If you want it to be required to be True
, the usual form field overrides apply - declare the field yourself or set its required
attribute to be True
somewhere before validating (I usually use the form's __init__
method). If you want it to allow True
or False
but not Python None
, it's harder.
具体来说, BooleanField
的标准小部件是一个复选框。浏览器不会为未选中的复选框提交任何内容,因此复选框小部件会将提交中不存在的字段视为 False
。无法将用户没有选择复选框与输入确实很糟糕的情况区分开。您可以使用其他小部件(例如, RadioSelect
)至少使浏览器可以提交 False
,但仍然存在问题,即 BooleanField
的 to_python
方法将其值转换为布尔值
,然后进行验证,因此您必须继承并覆盖。
Specifically, the standard widget for a BooleanField
is a checkbox. Browsers do not submit anything for unchecked checkboxes, so the checkbox widget treats the absence of the field from the submit as False
. There's no way to distinguish the user not selecting the checkbox from cases in which the input really is bad. You could use a different widget (say, a RadioSelect
) to at least make it possible for the browser to submit something for False
, but you still have the problem that the BooleanField
's to_python
method converts its value to a boolean
before validating, so you'd have to subclass and override.
幸运的是,错误验证代码不会将code>视为空,因此您可以设置
required = True
并且不需要自定义清洗方法
Fortunately, False
is not considered empty by the validation code, so if you do that you'll be able to set required=True
and don't need custom cleaning methods.
这篇关于django ModelForm似乎不验证BooleanField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!