从Fields数据中获取None而不是空字符串

Clo*_*eto 7 python wtforms flask-wtforms

我在WTForms表格中有这个字段

name = StringField('Name', validators = [Optional(), Length(max = 100)])
Run Code Online (Sandbox Code Playgroud)

当字段提交为空form.name.data时,将按预期包含空字符串.

有没有办法让它返回None而不是空字符串?这只是因为null在数据库中处理非常方便,如下所示update:

update t
set 
    name = coalesce(%(name)s, name),
    other = coalesce(%(other)s, other)
Run Code Online (Sandbox Code Playgroud)

使用上面的代码,我不需要检查字段是否为空,并在SQL代码中的Python代码中采取相应的操作.在nullcoalesce解决了容易.

Clo*_*eto 18

构造函数有filters参数Field

name = StringField(
    'Name', 
    validators = [Optional(), Length(max = 100)], 
    filters = [lambda x: x or None]
)
Run Code Online (Sandbox Code Playgroud)

http://wtforms.readthedocs.org/en/latest/fields.html#the-field-base-class