Retrieve and Update the User
Let’s learn how to get and update a user on the basis of the username.
We'll cover the following
Retrieve the user
Say we want to retrieve a specific user object. To do that using the username
, we will do the following:
user = User.objects.get(username='Ulrich')
This will give us our user and store it in the user
model. In the same way, we can also get the user back using the email
field.
Update the user
Now we want to update the user, which we just retrieved. Say we want to update the email. Here is how we will do that:
user.email="helsinki@gmail.com"
user.save()
This will simply update the user. Note how we are saving it, using the save()
function itself.
Get hands-on with 1400+ tech skills courses.