插入变量并循环python类调用

时间:2014-06-20 15:28:06

标签: python class flask flask-sqlalchemy flask-wtforms

我正在使用flask-sqlalchemy和flask-wtf。基本上我想获取由我的“row.form_field”查询生成的字符串并将其放入我的wtf表单类调用中。关于如何解决这个问题的任何想法?

from forms import form_list
from models import form_value

form = form_list

values = form_value.query.filter_by(form_entry_id = id)
for row in values:
   print row.form_field, row.value #This works.
   form.row.form_field.data = row.value #This does not!
   # form.???how to insert "row.form_field" here???.data = row.value

1 个答案:

答案 0 :(得分:1)

您可以使用getattr

getattr(form, row.form_field).data = row.value

但更简单,您只需将数据传递给实例化的WTForms类:

data = {row.form_field: row.value for row in values}
form_list = FormList(**data)