flask wtform字段在类方法中的访问



我已经定义了如下的表单:

class DisForm(FlaskForm):
dtype = SelectField("dtype", choices=[])

def __init__(self, **kwargs):

choices = []
dtypes = DTypes.query.all()
for t in dtypes:
item = (t.name, t.title)
choices.append(item)
self.dtype.choices = choices # ?????????

我想在init中填充字段的choices!我该怎么做呢?使用self.dtype不工作

我是这样解决的。在init中首先调用super似乎很重要。

class DisForm(FlaskForm):
dtype = SelectField("dtype", choices=[])

def __init__(self, **kwargs):
# This is important to call super first
super(DisForm, self).__init__(**kwargs)

choices = []
dtypes = DTypes.query.all()
for t in dtypes:
item = (t.name, t.title)
choices.append(item)
self.dtype.choices = choices

最新更新