如何解析WTForms的FieldList提交的字典数据并写入数据库
一、结构化数据获取方案
无需手动用正则解析提交的原始字典,只要正确定义WTForms的嵌套结构,直接调用表单实例的data属性就能直接拿到按索引分组的结构化列表:
from wtforms import Form, StringField, FloatField, FieldList, FormField, HiddenField # 定义单条步骤对应的子表单 class RecipeStepForm(Form): recipe_step_id = HiddenField() recipe_step_name = StringField("步骤名称") recipe_step_temp = FloatField("步骤温度") # 外层表单嵌套FieldList class RecipeForm(Form): stepset = FieldList(FormField(RecipeStepForm), min_entries=1)
接收请求初始化表单后,直接取form.stepset.data就能得到目标结构:
# Flask框架下的示例代码 form = RecipeForm(request.form) if form.validate_on_submit(): steps = form.stepset.data # 此时steps结构如下: # [ # {"recipe_step_id": "5", "recipe_step_name": "boil rice", "recipe_step_temp": 42.0}, # {"recipe_step_id": "6", "recipe_step_name": "heat rice", "recipe_step_temp": 57.0} # ] # 遍历存入数据库即可 for step in steps: # 新增/更新逻辑示例(SQLAlchemy) if step.get("recipe_step_id"): # 已有记录执行更新 exist_step = RecipeStep.query.get(step["recipe_step_id"]) exist_step.name = step["recipe_step_name"] exist_step.temp = step["recipe_step_temp"] else: # 无ID判定为新增记录 new_step = RecipeStep( name=step["recipe_step_name"], temp=step["recipe_step_temp"], recipe_id=当前关联菜谱ID ) db.session.add(new_step) db.session.commit()
如果确实需要手动处理原始提交字典,可以用字符串分割的方式实现,比正则更简单高效:
raw_data = { "stepset-0-recipe_step_id": "5", "stepset-0-recipe_step_name": "boil rice", "stepset-0-recipe_step_temp": "42.0", "stepset-1-recipe_step_id": "6", "stepset-1-recipe_step_name": "heat rice", "stepset-1-recipe_step_temp": "57.0", } step_group = {} for key, val in raw_data.items(): parts = key.split("-") if len(parts) != 3 or parts[0] != "stepset": continue idx = int(parts[1]) field_name = parts[2] if idx not in step_group: step_group[idx] = {} step_group[idx][field_name] = val # 转换为有序列表 step_list = [step_group[i] for i in sorted(step_group.keys())]
二、ID字段相关问题解答
- 是否需要携带ID字段:如果是编辑已有步骤的场景,ID是必要的,否则无法区分提交的内容对应哪条数据库记录,只能全删旧记录再新增,既消耗性能也容易丢失关联数据。纯新增场景可以不带ID。
- 是否需要设置为隐藏字段:必须设为
HiddenField,既不会在前端展示给用户,也不支持用户直接编辑。如果担心用户篡改隐藏字段的值,可以额外对ID做签名校验,避免非法提交。
内容的提问来源于stack exchange,提问作者jt196
相关产品推荐
相关产品推荐

