How to update multiple records using peewee

廉价感情. 提交于 2019-12-23 15:12:01

问题


I'm using Peewee with Postgres database. I want to know how to update multiple records in a tabel at once?
We can perform this update in SQL using these commands, and I'm looking for a Peewee equivalent approach.


回答1:


ORMs usually dose not support bulk update and you have to use custom SQL, you can see samples in this link (db.excute_sql)




回答2:


Yes, you can use the insert_many() function:

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.

Example:

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

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

More details at: http://docs.peewee-orm.com/en/latest/peewee/api.html#Model.insert_many



来源:https://stackoverflow.com/questions/41028373/how-to-update-multiple-records-using-peewee

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!