我正在WTForms
使用Flask
,我需要在字段基类中添加一个新参数。到目前为止,我曾经通过调用一个返回所需元组的函数来为description
参数指定一个元组:
# app/forms.py
# -*- coding: utf-8 -*-
from flask.ext.wtf import Form
from wtforms import IntegerField
from wtforms import ValidationError
from wtforms.validators import InputRequired
from flask.ext.babel import lazy_gettext
def descr(name):
descrs = {
'd': ('description', 'unit'),
'h': (lazy_gettext('description2'), 'unit2'),
# ...
}
return descrs.get(name, name)
class As(Form):
d = IntegerField('d', default=210, description=descr('d'), validators=[InputRequired()])
# ...
class BendingForce(Form):
h = IntegerField(lazy_gettext('h'), default=250, description=descr('h'), validators=[InputRequired(), grater_than_cero])
# ...
现在我希望有两个参数description
和unit
来分配所需的值。我怎么能做到这一点?无论如何这样做是明智的吗?
示例:
class BendingForce(Form):
h = IntegerField(lazy_gettext('h'), default=250,
description='description2', unit='unit2',
validators=[InputRequired(), grater_than_cero])
# ...