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:
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?