I am trying to update a user in a Django database.
The data obtained is as follows:
fetched_data = {
'id': 1,
'first_name': 'John',
'last_name': 'Doe',
'phone': '+32 12',
'mobile_phone': '+32 13',
'email': 'myemail@hotmail.com',
'username': 'myusername'
}
I get a user with this id as follows:
old_user = User.objects.get(pk=fetched_data['id'])
If I update the user as follows:
old_user.username = fetched_data['username']
old_user.first_name = fetched_data['first_name']
......
old_user.save()
it works fine, but I don't want to do this for every entry, so I tried something like:
for fetched_data_key in fetched_data:
old_user.fetched_data_key = fetched_data['fetched_data_key']
//old_user[fetched_data_key] = fetched_data['fetched_data_key'] --- I tried this way to
old_user.save()
But that does not work. Any idea how I can update a user without doing this for each entry?
source
share