【发布时间】:2015-04-07 21:34:31
【问题描述】:
我正在尝试使用wtforms 检查字典中的数据是否属于所需类型。在下面的示例中,我想确保字典中的 some_field 是一个整数。文档让我相信,如果我使用IntegerField,数据将被强制转换为整数,StringField 将被强制转换为字符串等。但是,foo.validate() 会返回True,即使@987654328 的类型也是如此@ 不是整数。这是预期的行为,为什么?如果这是预期的行为,是否可以根据需要使用wtforms 进行类型验证?
>>> from wtforms import Form, IntegerField, validators
>>> class Foo(Form):
... some_field = IntegerField(validators=[validators.Required()])
>>> foo = Foo(**{'some_field':'some text input'})
>>> foo.data
{'some_field': 'some text input'}
>>> foo.validate()
True
>>> IntegerField?
Type: type
String form: <class 'wtforms.fields.core.IntegerField'>
File: c:\users\appdata\local\continuum\anaconda\envs\env\lib\site-packages\wtforms\fields\core.py
Init definition: IntegerField(self, label=None, validators=None, **kwargs)
Docstring:
A text field, except all input is coerced to an integer. Erroneous input
is ignored and will not be accepted as a value.
【问题讨论】: