我有一张表格,我曾用它来输入配方中的选择成分。我使用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 = 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)
然后我将选择的结果存储在Recipeingredients表中,该表包含3个字段(id,recipe和许多成分)
class Recipeingredients(db.Model):
"""
#Not sure how to store my list of ingredient
#I need to obviously store the ingredient_id from Ingredientlist.
#That might be enough since from the ingredient_id I know the rest...
"""
id = db.Column(db.Integer, primary_key=True)
recipe = db.Column(db.String(60), index=False, unique=False)
ingredient_id = db.relationship( 'Ingredientlist', backref='ingredientlist.id', secondary='taxonomy' )
def __init__(self, recipe, ingredient_id):
self.recipe = recipe
self.ingredient_id = ingredient_id
taxonomy = db.Table(
'taxonomy',
db.Column( 'recipe_id', db.Integer, db.ForeignKey( 'recipeingredients.id' ) ),
db.Column( 'ingredient_id', db.Integer, db.ForeignKey( 'ingredientlist.id' ) ),
)
该部分正在运行,然后我可以显示特定食谱的内容。
接下来我正在尝试编辑该配方。我不知道该怎么办。 这是我做的: 1 /我查询Recipeingredient表 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}]}
我可以看到问题是因为Recipeingredient的查询与表单的预期不符,但我看不出如何使其匹配。
我是否想从查询中重新创建数据,以使其与表单所期望的相匹配?还是有更好的方法来实现它?