I have this field in the WTForms
form
name = StringField('Name', validators = [Optional(), Length(max = 100)])
When the field is submitted empty then form.name.data
will, as expected, contain an empty string.
Is there some way to make it return None
in instead of an empty string? It is just because it is very convenient to deal with null
in the database like in this update
:
update t
set
name = coalesce(%(name)s, name),
other = coalesce(%(other)s, other)
With the above code I don't need to check if the field is empty or not and take actions accordingly be it in the Python code on in the SQL code. The null
with the coalesce
solves that easily.
None
yourself? E.g.whatever_database.insert_record(form.name.data or None, form.other.data or None)
– PandemoniumStringField
. This is counter-intuitive IMO, since it means there's no way to distinguish between 'user did not enter a value for this field' and 'user submitted an empty string', which might have very different semantics in an application (it does in mine). – Usurer