Using Custom User model in Django
Learn how to create a custom User model in Django to implement more fields and functionality to the existing User model.
While the default User model in Django is powerful and contains essential fields already, we are often required to add more fields and feature to the existing User model. To solve this specific problem, creating a custom User model in Django is a recommended practice. In this detailed guide, we will see how to create a custom user model by extending Django's user model. We will also discuss when to use a custom user model and when to use the default user model.
Extended User Information : If your application requires more information about the user, and needs more fields to do so, then you should create a custom user model.
Custom Authentication : Django's default User model uses
usernamefield for authentication, if your application requires you to use any other field for authentication such as
phone_numberthen also you should use a custom user model.
Future Improvements : If your are uncertain about the fields in your user model, and feel that you will add more fields to the user model in the future, then you should start with a custom user model.
Let's dive in to the steps to create a custom user model in Django:
Start a new Django Project : Let's start a new project, it's always a good practice to understand the future scope and create a custom user model in the start of project if possible. Switching to a custom user model mid-project can be a complex and time consuming process.
django-admin startproject myproject cd myproject
Create a New Django App : It's recommended to keep the custom user model in a separate app, so let's create a new app for it.
python manage.py startapp users
Define the Custom User Model : In the
usersapp, create a model that extends the default Django user model, either by using
AbstractBaseUser. We recommend using
AbstractUseras it includes the Django's default user model fields and methods. If you are using
AbstractBaseUserthen you will have to define all the fields and methods yourself, as it doesn't include any of the default user model fields and methods.
Now let's define the custom user model in
from django.contrib.auth.models import AbstractUser from django.db import models class CustomUser(AbstractUser): # Add additional fields here if needed age = models.PositiveIntegerField(null=True, blank=True)
Update Django Settings file : Once you've defined the Custom user model, you also havbe to update django settings file to use the custom user model. Open file
myproject/settings.py, and the following change :
AUTH_USER_MODEL = 'users.CustomUser'
Create a User Manager : You can skip this stepm if you have extended
AbstractUserclass, however if you've extended
AbstractBaseUseryou will need to create a custom user manager as well, to define how to create users and superusers.
Run Migrations : Run the migrations to create the custom user model that we just created :
python manage.py makemigrations python manage.py migrate
and that's all. Those were all the steps required to create a custom user model.
Custom user model is one of the most sought-after change in Django, and it's a clean way to add more fields and functionality to the existing user model. As we learnt in this tutorial, it's recommended that we adopt a custom user model from the very start of the project.
Feel free to comment if you have any questions or suggestions. As always, keep learning 🤞
Get in Touch