django.contrib.auth.models.User
). This default model is sufficient for many basic web applications. However, in real - world scenarios, you often need more flexibility and additional fields to meet specific requirements. For instance, you might want to use an email address as the primary identifier instead of a username, or you may need to add custom fields like a user’s date of birth, profile picture, or social media links. In such cases, creating a custom user model in Django becomes essential. This blog post will guide you through the process of creating a custom user model, explain the core concepts, discuss typical usage scenarios, highlight common pitfalls, and share best practices.password
and last_login
). You have to define all other fields (such as username
, email
, etc.) and implement methods like get_username
, is_active
, etc. yourself. It gives you the most flexibility but requires more coding.AbstractBaseUser
and includes more fields like username
, first_name
, last_name
, email
, etc. It also has pre - implemented methods for common user operations. You can extend it by adding your own custom fields.A UserManager
is a class that provides methods for creating users. When you create a custom user model, you often need to define a custom UserManager
to handle user creation and management. For example, you might want to override the create_user
and create_superuser
methods to handle custom fields during user creation.
First, create a new Django app where you will define your custom user model.
python manage.py startapp customuser
In the models.py
file of the customuser
app, define your custom user model. Here is an example of extending AbstractUser
:
# customuser/models.py
from django.db import models
from django.contrib.auth.models import AbstractUser
class CustomUser(AbstractUser):
# Add custom fields here
date_of_birth = models.DateField(null=True, blank=True)
phone_number = models.CharField(max_length=15, null=True, blank=True)
def __str__(self):
return self.username
# customuser/models.py
from django.contrib.auth.models import BaseUserManager
class CustomUserManager(BaseUserManager):
def create_user(self, username, email, password=None, **extra_fields):
"""
Create and save a regular user with the given username, email, and password.
"""
if not email:
raise ValueError('The Email field must be set')
email = self.normalize_email(email)
user = self.model(username=username, email=email, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, username, email, password=None, **extra_fields):
"""
Create and save a superuser with the given username, email, and password.
"""
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
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(username, email, password, **extra_fields)
# Update the CustomUser model to use the custom manager
class CustomUser(AbstractUser):
date_of_birth = models.DateField(null=True, blank=True)
phone_number = models.CharField(max_length=15, null=True, blank=True)
objects = CustomUserManager()
def __str__(self):
return self.username
In your project’s settings.py
file, specify the custom user model:
# settings.py
AUTH_USER_MODEL = 'customuser.CustomUser'
python manage.py makemigrations
python manage.py migrate
create_user
and create_superuser
methods in the UserManager
, these fields may not be properly populated during user creation.AUTH_USER_MODEL
setting in your settings.py
file will result in Django still using the default user model.Creating a custom user model in Django gives you the flexibility to meet the specific requirements of your web application. By understanding the core concepts, following the proper steps, and avoiding common pitfalls, you can effectively implement a custom user model. Remember to plan ahead, test thoroughly, and refer to Django’s official documentation for guidance.
This blog post should provide you with a comprehensive understanding of creating a custom user model in Django and help you apply it in real - world projects.