Jquery 使用多对多查询填充和编辑表单
我有一个表格,我用来输入配方中的精选配料。我使用jquery脚本来“克隆” 表格,以便我可以输入多个成分Jquery 使用多对多查询填充和编辑表单,jquery,flask,many-to-many,flask-sqlalchemy,flask-wtforms,Jquery,Flask,Many To Many,Flask Sqlalchemy,Flask Wtforms,我有一个表格,我用来输入配方中的精选配料。我使用jquery脚本来“克隆” 表格,以便我可以输入多个成分 class IngredientListTemplate(Form): """ Template to add a new ingredient to a recipe The 2 input are prefilled with the data from the query_factory """ category = QuerySelectFie
class IngredientListTemplate(Form):
"""
Template to add a new ingredient to a recipe
The 2 input are prefilled with the data from the query_factory
"""
category = QuerySelectField(query_factory=categorylist, get_label=u'category', allow_blank=False)
ingredient = QuerySelectField(query_factory=ingredientlist, get_label=u'ingredient', allow_blank=False)
def __init__(self, *args, **kwargs):
kwargs['csrf_enabled'] = False
super(IngredientListTemplate, self).__init__(*args, **kwargs)
class IngredientListForm(Form):
"""
Display the template
"""
ingredient_list = FieldList(FormField(IngredientListTemplate), min_entries=1)
然后,我将选择的结果存储在RecipeElements表中,该表有3个字段(id、recipe和多对多配料)
该部分正在工作,然后我可以显示特定配方的内容
接下来,我将尝试编辑该配方。我不知道如何进行。
以下是我的工作:
1/I查询RecipeCredit表
2/我将其传递给表单,然后呈现模板
@mod.route('/ingredients_selection/<int:recipe_id>', methods=['GET', 'POST'])
def ingredient_selection(recipe_id):
recipe = Recipeingredients.query.get(recipe_id)
#print recipe.ingredient_id #returns [5, 6, 9, 10, 19, 21, 23]
form = IngredientListForm(obj=recipe)
print form.data # returns {'ingredient_list': [{'category': None, 'ingredient': None}]}
@mod.route('/components\u selection/',methods=['GET','POST'])
def成分选择(配方id):
配方=RecipeIngElements.query.get(配方id)
#打印recipe.component_id#返回[5,6,9,10,19,21,23]
表单=IngredientListForm(obj=配方)
print form.data#返回{'component_list':[{'category':None,'component':None}]}
我可以看出问题是因为RecipeInCredit的查询与表单所期望的不匹配,但我看不到如何使其匹配
我是否要从查询中重新创建数据,使其与表单所期望的内容相匹配?还是有更好的方法来实现它
@mod.route('/ingredients_selection/<int:recipe_id>', methods=['GET', 'POST'])
def ingredient_selection(recipe_id):
recipe = Recipeingredients.query.get(recipe_id)
#print recipe.ingredient_id #returns [5, 6, 9, 10, 19, 21, 23]
form = IngredientListForm(obj=recipe)
print form.data # returns {'ingredient_list': [{'category': None, 'ingredient': None}]}