首页 > 解决方案 > 如何在python中迭代sql aclchemy结果

问题描述

我尝试了多种不同的东西和在其他帖子中找到的各种建议。我无法让它工作,所以我希望有人能指出我正确的方向。

我的帖子部分不起作用。它不会更新我的库存,它返回的页面不再包含 ID。但是 POST 之前的页面可以正常工作。

让烧瓶应用程序使用sqlalchemyand python

我想迭代查询中仅针对一列返回的结果,但我知道炼金术正在返回所有列。基本上,对于基于配方返回的每个成分 ID,我需要更新该成分 ID 的总库存。

recipe_id= request.args.get('id')
recipe = Recipe_ingredients.query.filter_by(recipe_id=recipe_id).all()

if request.method == 'POST':
    for row in recipe:
        ingredient_id = row.ingredient_id
        inventory = Inventory.query.filter_by(ingredient_id=ingredient_id).first()
        inventory.grams = inventory.grams - recipe.grams
        inventory.ounces = inventory.ounces - recipe.ounces
        db.session.commit()

inventory = Inventory.query.all()
return render_template('recipe.html', recipe=recipe, inventory=inventory)

标签: pythonflask-sqlalchemy

解决方案


Does this script returns execution error? Or data is not updated in database? Because I'm afraid, that you have missing indentation - everything, what is inside of for row in recipe: block, must have one more level of indentation, to be executed in the for loop...


推荐阅读