Stack Overflow for Teams is a private, secure spot for you and
your coworkers to find and share information.
Learn more
I'm trying to update many records inside a table using
Peewee
library. Inside a
for
loop, i fetch a single record and then I update it but this sounds awful in terms of performance so I need to do the update in bulk. Current code look like this:
usernames_to_update = get_target_usernames()
for username in usernames_to_update:
user = User.get(User.username == username) # username is primary key
if user.type == 'type_1':
user.some_attr_1 = some_value_1
elif user.type == 'type_2':
user.some_attr_2 = some_value_2
# elif ....
user.save()
In the documentation, there is insert_many
function but nothing like update_many
. Searching around i came up with these solutions:
Executing raw query using CASE
: Link
Using replace_many
: Link
Using update
: Link
But i couldn't find any examples of how to use the second or third solution. Can somebody clarify how cases 2 and 3 can be used?
You want the .update() method:
query = User.update(validated=True).where(User.username.in_(usernames_to_update))
query.execute()
Edit: so you want to conditionally set the value during an update. You can use the Case
helper. Untested:
some_value_1 = 'foo'
some_value_2 = 'bar'
case_stmt = Case(User.type, [
('type_1', some_value_1),
('type_2', some_value_2)])
query = User.update(some_field=case_stmt).where(User.username.in_(list_of_usernames))
query.execute()
Docs can be found here: http://docs.peewee-orm.com/en/latest/peewee/api.html#Case
–
–
–
–
The new best answer is to use the bulk_update()
method found here:
with database.atomic():
User.bulk_update(user_list, fields=['username'], batch_size=50)
–
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.