A superuser in django can be created using the following command :
python manage.py createsuperuser
When you try to execute this command, it will give an error like this. This is createsuperuser command requries the username field for creating an object in the database. In order to create a superuser, you would need to override the BaseUserManager
class.
This can be done as follows :
from django.db import models
from django.contrib.auth.models import AbstractUser
from django.contrib.auth.base_user import BaseUserManager
from django.contrib.auth.hashers import make_password
class UserManager(BaseUserManager):
use_in_migrations = True
def _create_user(self, email, password, **extra_fields):
if not email:
raise ValueError('Users require an email field')
email = self.normalize_email(email)
user = self.model(email=email, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user
def create_user(self, email, password=None, **extra_fields):
extra_fields.setdefault('is_staff', False)
extra_fields.setdefault('is_superuser', False)
return self._create_user(email, password, **extra_fields)
def create_superuser(self, email, password, **extra_fields):
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
extra_fields.setdefault('role', 'a')
if extra_fields.get('is_staff') is not True:
raise ValueError('Superuser must have is_staff=True.')
if extra_fields.get('is_superuser') is not True:
raise ValueError('Superuser must have is_superuser=True.')
return self._create_user(email, password, **extra_fields)
class User(AbstractUser):
username = None
email = models.EmailField('Email Address', unique=True)
profile_photo = models.ImageField(upload_to='profile_photos/', null=True, blank=True,default='profile_photos/default_profile.png')
mobile_number = models.CharField(max_length=10, null=True, blank=True)
role = models.CharField(max_length=1, choices=[('a', 'Admin'), ('h', "HOD"), ('s', 'Student')])
objects = UserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
def save(self, *args, **kwargs):
if not self.password.startswith('pbkdf2_sha256$'):
self.password = make_password(self.password)
super().save(*args, **kwargs)
Here, we are overriding the functions inside the BaseUserManager class to accept email field and create users based on that field.
Now, when you try to createsuperuser you won't get that error.
python manage.py createsuperuser
And here superuser has been created successfully. You can use this to login inside the admin portal. First you would need to start the server which can be done using the following command :
python manage.py runserver 0.0.0.0:8000
here 0.0.0.0
refers to the ip at which the project is launched 0.0.0.0
means it can be served on any ip address and 8000
is the port number at which it is served.
Now you can go to admin page by visiting the following url : http://localhost:8000/admin/login/?next=/admin/ By typing the email and password, you would be able to login to the website. You would get a page like this when you login to the admin site. Here still the custom User model is not visible as it has not been registered inside the admin portal. The Groups model that is being displayed here is used to provide access permissions for users in Django and is a default model created by django.
In the next module, you will see how you can register, view and edit User model through admin page.