如何在python中迭代sql acchemy结果

2024-09-28 22:35:48 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我的帖子部分不起作用。它不会更新我的库存,它返回的页面不再包含ID。不过,文章之前的页面工作得很好。在

使用sqlalchemypython使用flask app。在

我想迭代查询中只返回一列的结果,但我知道alchemy返回的是所有列。基本上,对于根据配方返回的每个成分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)

Tags: idbyrequest库存recipe页面filterquery
1条回答
网友
1楼 · 发布于 2024-09-28 22:35:48

这个脚本是否返回执行错误?或者数据库中的数据没有更新? 因为我担心,你遗漏了缩进——所有的东西,包括for row in recipe:块内的内容,都必须有一个更高的缩进级别,才能在for loop中执行。。。在

相关问题 更多 >