【发布时间】:2013-07-11 20:16:32
【问题描述】:
我正在创建一个产品编辑表单,我需要在表单中预先填充以前的数据。
我正在做以下事情:
Product(form):
product = TextField('name')
category = SelectField('category', choice=[(1,'one'),(2,'two')])
在视图中:
form.product.data = 'A product name from the database'
form.category.data = 'a category name' #This does not work
问题出在 SelectField 上。
我知道我可以在 SelectField 上设置一个“默认”值。但是,这发生在表单定义类中,我还没有来自 sqlalchemy 的查询对象。
那么,有没有办法在运行时在选择字段上附加默认值?
【问题讨论】:
-
form.category.data = 1或form.category.data = 2 -
我试过了。不工作。
-
正如 falsetru 所说,但在将强制设置为 int 之前:
category = SelectField('category', choice=[(1,'one'),(2,'two')], coerce=int)。 form 是 Product Form 实例。