python - 如何在python中迭代sql aclchemy结果
问题描述
我尝试了多种不同的东西和在其他帖子中找到的各种建议。我无法让它工作,所以我希望有人能指出我正确的方向。
我的帖子部分不起作用。它不会更新我的库存,它返回的页面不再包含 ID。但是 POST 之前的页面可以正常工作。
让烧瓶应用程序使用sqlalchemy
and 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)
解决方案
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
...
推荐阅读
- javafx - 使用 javafx PrinterJob 打印 ListView
- c# - 无效的十进制验证
- windows - 如何以管理员身份以编程方式运行 PowerShell?
- node.js - “npm install”和“yarn install”都以错误告终
- android - 在Android中录制视频时显示textview
- java - 不兼容的类型 Object 不能转换成 Throwable?
- javascript - 当 iframe 改变时执行函数
- vue.js - 为什么 v-model 会改变 Vuex 状态而不是组件的本地数据?
- console - 服务现在公告 500(内部服务器错误)
- sql - 返回跨年份边界的前几个月数据 - SQL Server