如何使用peewee更新多条记录

How to update multiple records using peewee

我正在使用带有 Postgres 数据库的 Peewee。我想知道如何一次更新一个表中的多条记录?
我们可以使用 these commands 在 SQL 中执行此更新,我正在寻找一种 Peewee 等效方法。

是的,您可以使用insert_many()函数:

Insert multiple rows at once. The rows parameter must be an iterable that yields dictionaries. As with insert(), fields that are not specified in the dictionary will use their default value, if one exists.

示例:

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

FORMs 通常不支持批量更新,您必须使用自定义 SQL,您可以在此 link (db.excute_sql)

中查看示例