Published on

Using Custom User model in Django

Authors

Introduction

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.

Table of Contents

When to Use a Custom User Model in Django ?

  1. 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.

  2. Custom Authentication : Django's default User model uses username field for authentication, if your application requires you to use any other field for authentication such as email_address or phone_number then also you should use a custom user model.

  3. 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.

How to Create a Custom User Model in Django?

Let's dive in to the steps to create a custom user model in Django:

  1. 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
    
  2. 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
    
  3. Define the Custom User Model : In the users app, create a model that extends the default Django user model, either by using AbstractUser or AbstractBaseUser. We recommend using AbstractUser as it includes the Django's default user model fields and methods. If you are using AbstractBaseUser then 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 users/models.py file.

    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)
    
  4. 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'
    
  5. Create a User Manager : You can skip this stepm if you have extended AbstractUser class, however if you've extended AbstractBaseUser you will need to create a custom user manager as well, to define how to create users and superusers.

  6. 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.

Conclusion

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 🤞

Schedule·a·Technical·Requirements·Consultation·call·with·me.
Share on :  FacebookTwitterLinkedInWhatsapp