Subclassing AbstractBaseUser
is the recommended way to create a custom user model
from django.contrib.auth.models import AbstractBaseUser
class CustomUser(AbstractBaseUser):
pass
Create a custom manager for the custom user model by subclassing BaseUserManager
from django.contrib.auth.models import BaseUserManager
class CustomUserManager(BaseUserManager):
pass
Add fields to the custom user model, such as email, username and password
from django.db import models
class CustomUser(AbstractBaseUser):
email = models.EmailField(unique=True)
username = models.CharField(max_length=40, unique=True)
password = models.CharField(max_length=100)
full_name = models.CharField(max_length=100)
phone_number = models.CharField(max_length=15)
Add the custom manager to the custom user model
class CustomUser(AbstractBaseUser):
email = models.EmailField(unique=True)
username = models.CharField(max_length=40, unique=True)
password = models.CharField(max_length=100)
full_name = models.CharField(max_length=100)
phone_number = models.CharField(max_length=15)
objects = CustomUserManager()
Update the settings.py file to use the custom user model
AUTH_USER_MODEL = 'your_app.CustomUser'
Create migration for the custom user model
python manage.py makemigrations
Apply the migration
python manage.py migrate