forgotten credentials (superuser and password) in Django project

Viewed 180

Forgotten superuser name and password in Django project. What is the correct way to recover it specially the superuser name? Is it a good practice to create a new superuser to get to admin panel and recover the forgotten superuser name from there or there is another way to recover it in terminal?

2 Answers
python manage.py changepassword <username>

You can get the usernames (but not the password since it is hashed) of super users by making a query in the shell (python manage.py shell), while doing this you can also change their password to something new if you want by using the set_password method:

from django.contrib.auth import get_user_model


UserModel = get_user_model()
superusers = UserModel.objects.filter(is_superuser=True)
for user in superusers:
    print(user.username)
    # Want to reset their password? Run below line
    user.set_password('new_password')

You can of course create a new superuser using python manage.py createsuperuser if you want to.

Related