如何使用 peewee 更新多条记录

2024-04-20

我正在使用 Peewee 和 Postgres 数据库。我想知道如何一次更新一个表中的多条记录?
我们可以使用 SQL 来执行此更新这些命令 https://stackoverflow.com/questions/20255138/sql-update-multiple-records-in-one-query,我正在寻找 Peewee 的等效方法。


是的,您可以使用insert_many()功能:

一次插入多行。 rows 参数必须是可迭代的 这会产生字典。与 insert() 一样,不是的字段 字典中指定的将使用它们的默认值,如果有的话 存在。

Example:

usernames = ['charlie', 'huey', 'peewee', 'mickey']
row_dicts = ({'username': username} for username in usernames)

# Insert 4 new rows.
User.insert_many(row_dicts).execute()

更多详情请参见:http://docs.peewee-orm.com/en/latest/peewee/api.html#Model.insert_many http://docs.peewee-orm.com/en/latest/peewee/api.html#Model.insert_many

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 peewee 更新多条记录 的相关文章

随机推荐