Creating and Managing Superusers
Superusers are users who have all permissions and can manage all aspects of a Django application via the admin interface. In this section, we'll cover how to create and manage superusers in your custom User model.
Creating a Superuser
To create a superuser with your custom User model, use the createsuperuser
management command:
python manage.py createsuperuser
During the creation process, you will be prompted to enter a username, email address, and password. Ensure that the password meets the validation criteria defined in your settings.
Managing Superusers in the Admin Interface
After creating a superuser, you can log into the admin interface using the credentials you provided. You can manage users, groups, permissions, and site settings from the admin dashboard.
Updating Superuser Status
If you need to modify an existing user's superuser status, you can do this directly in the Django admin interface:
- Go to the Users section in the admin panel.
- Select the user you want to edit.
- Check the boxes for
Staff status
andSuperuser status
to grant those permissions.
Alternatively, you can update the user programmatically:
user.is_superuser = True
user.is_staff = True
user.save()
Best Practices
While superusers have powerful permissions, it’s essential to limit the number of superusers to reduce security risks. Only assign superuser status to trusted individuals and regularly review user permissions.