我有很长的wtform:
即
structlog.configure
当用户提交表单时,Flask将检索每个问题的数据:
即
Question1 = TextAreaField('What is your name?')
...
Question100=TextAreaField('Describe your role')
然后我使用sql将数据写入db 即
Question1=form.Question1.data
..
Question100=form.Question100.data
SQL注入不是问题,我希望不要使用SQLalchemy。有没有更有效的方法可以做到这一点?
编辑: 假设某些字段类型是FieldLists,Formfields或HiddenFields。我该如何将它们写到表中?我怎么可能跳过它们?
答案 0 :(得分:1)
快速可读的解决方案
written_fields = [f for f in form if f.id in WRITE_TO_SQL_FIELDS]
cursor.execute(
"INSERT INTO table ({}) values ({})".format(
",".join(f.id for f in written_fields),
",".join(str(f.data) for f in written_fields),
)
)
或者,如果您希望按类型进行过滤:
BLACKLISTED_TYPES = (wtforms.FieldList, wtforms.Formfield, wtforms.HiddenField)
written_fields = [f for f in form if not isinstance(f, BLACKLISTED_TYPES)]