Flask-SQLalchemy 更新一行信息
如何更新行的信息?
例如,我想更改 id 为 5 的行的名称列。
How can I update a row's information?
For example I'd like to alter the name column of the row that has the id 5.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我一直在寻找比 @Ramesh 的答案(很好)但仍然充满活力的东西。这是一个将更新方法附加到 db.Model 对象的解决方案。
您传入一个字典,它只会更新您传入的列。
然后在路线中您可以执行以下操作
I was looking for something a little less intrusive then @Ramesh's answer (which was good) but still dynamic. Here is a solution attaching an update method to a db.Model object.
You pass in a dictionary and it will update only the columns that you pass in.
Then in a route you can do
更新烧瓶中的列
Update the Columns in flask
解决方案(会话)/在 Flask_sqlalchemy (db) 中:
db.session.query(BlogPost).where(BlogPost.id == post_id).
更新({
“attribute01”:新数据,
“attribute02”:新数据
})
db.session.commit()
Solution (session) / in Flask_sqlalchemy (db):
db.session.query(BlogPost).where(BlogPost.id == post_id).
update({
"attribute01": new_data,
"attribute02": new_data
})
db.session.commit()
使用Flask-SQLAlchemy 文档中显示的教程检索对象。获得要更改的实体后,请更改实体本身。然后,
db.session.commit()
。例如:
Flask-SQLAlchemy 基于 SQLAlchemy,因此请务必查看 SQLAlchemy 文档 。
Retrieve an object using the tutorial shown in the Flask-SQLAlchemy documentation. Once you have the entity that you want to change, change the entity itself. Then,
db.session.commit()
.For example:
Flask-SQLAlchemy is based on SQLAlchemy, so be sure to check out the SQLAlchemy Docs as well.
SQLAlchemy 中的 BaseQuery 对象有一个方法
update
,该方法由filter_by
返回。当有许多对象需要更新时,使用
update
相对于更改实体的优势就显现出来了。如果您想向所有
admin
授予add_user
权限,请注意
filter_by
采用关键字参数(仅使用一个=),而不是采用表达式的
filter
。There is a method
update
on BaseQuery object in SQLAlchemy, which is returned byfilter_by
.The advantage of using
update
over changing the entity comes when there are many objects to be updated.If you want to give
add_user
permission to all theadmin
s,Notice that
filter_by
takes keyword arguments (use only one=
) as opposed tofilter
which takes an expression.如果您修改模型的 pickled 属性,这将不起作用。应替换 Pickled 属性才能触发更新:
This does not work if you modify a pickled attribute of the model. Pickled attributes should be replaced in order to trigger updates:
只需分配值并提交它们即可适用于除 JSON 和 Pickled 属性之外的所有数据类型。由于上面解释了 pickled 类型,因此我将记下一种略有不同但简单的更新 JSON 的方法。
假设模型如上所示。
这会将用户添加到带有数据 {"country":"Sri指定"} 的 MySQL 数据库中,
修改数据将被忽略。我的不起作用的代码如下。
我没有经历将 JSON 复制到新字典(而不是像上面那样将其分配给新变量)的痛苦工作,这本来应该有效,但我找到了一种简单的方法来做到这一点。有一种方法可以标记 JSON 已更改的系统。
以下是工作代码。
这就像一个魅力。
除了此方法之外,还提出了另一种方法此处
希望我对某人有所帮助。
Just assigning the value and committing them will work for all the data types but JSON and Pickled attributes. Since pickled type is explained above I'll note down a slightly different but easy way to update JSONs.
Let's say the model is like above.
This will add the user into the MySQL database with data {"country":"Sri Lanka"}
Modifying data will be ignored. My code that didn't work is as follows.
Instead of going through the painful work of copying the JSON to a new dict (not assigning it to a new variable as above), which should have worked I found a simple way to do that. There is a way to flag the system that JSONs have changed.
Following is the working code.
This worked like a charm.
There is another method proposed along with this method here
Hope I've helped some one.
,我们可以通过将 json 数据传递到更新查询中来动态更新记录:
In RestApi, We can update the record dynamically by passing the json data into update query:
要使用
update
方法(更新会话外的主菜),您必须按如下步骤查询对象:To use the
update
method (which updates the entree outside of the session) you have to query the object in steps like this: