How to Reset Admin and  Superuser Password in Django?

How to Reset Admin and Superuser Password in Django?

Editorial Team
Editorial Team

Django's built-in authentication provides everything we need to create and manage user accounts. One of the key users in Django's authentication system is the superuser. Superusers have access to the Django admin panel.

But, what if you forgot the admin password. How can you reset the password of admin users or superusers?

Reset Admin and Superuser Password

Resetting the password of any user in Django is quite simple. In this post, I will explain how to reset the password of any user using Django shell.

If you have not heard about Django shell before, it is a simple Python shell with which you can call the APIs of Django.

To open the Django shell, navigate to the folder where file exists. You can use the ls command to verify that file exists in the current directory. Then run this command to open a Django shell.

python shell

To reset the user password, first, we should import the user model. If you are using the default Django User model, the following code can be used to import the User model.

from django.contrib.auth.models import User

Most probably you will be using a custom user model. If yes, the above method will not work for you. For example, if you have created a custom user model in an app named accounts, use this command instead.

from accounts.models import User

Replace accounts with the name of your app and User with the name of your User model.

We can also use the get_user_model() method provided by Django to get access to the User model.

from django.contrib.auth import get_user_model
User  = get_user_model()

Find admin user accounts

All admin accounts can be fetched by passing is_superuser=True parameter to the filter method.

users = User.objects.filter(is_superuser=True)

Alternatively, if you know the email or username of the user, fetch the user object using:

user = User.objects.get(username='any_username_here')

Reset password

We can use the set_password() method to reset the password of any user.


The password of the user is now successfully changed. Make sure to choose a strong password.