Creating a Custom User Model in Django

Tram Ho

How to replace username with email in Django authentication?

This article will explain step-by-step how to create a custom User model in Django where email is used as a primary user instead of a username for authentication.

Note that doing this will greatly alter the database schema so it is advisable to work on a new project. For a project that has been developed in the past it is necessary to backup the data and recreate the DB.

Objectives

After this article you can learn:

  1. Explain the difference between AbstractUser and AbstractBaseUser
  2. Explain why it is necessary to set up a custom User model when starting a new Django project.
  3. Start a Django project with a custom User model
  4. Use an email address as a primary user instead of a username for authentication.
  5. Practice with test-first development when implementing a custom User model.

AbstractUser with AbstractBaseUser

By default, the User model in Django uses a username for authentication. Instead if you wish to use email, you will need to create a custom User model using either the AbstractUser or AbstractBaseUser subclassing.

Options

  1. AbstractUser: Use this option if you want to use the existing User fields and just want to remove the username field.
  2. AbstractBaseUser: Use this option if you want to create a completely new User model.

We will try both with these two options. The steps are the same for both options:

  1. Create a custom User model and Manager
  2. Update setting.py
  3. Customize UserCreationForm and UserChangeForm
  4. Update admin

It must be emphasized that the custom User model is used when starting a new Django project. If not for the new project we need to create another model like UserProfile and link it to Django’s User model with OneToOneField if you want to add a new field to the User model.

Project Setup

Proceed to create a new project with a users app.

Migrations have not been conducted. Remember, you need to create a custom User model before you apply for the first migration.

Add the app to the list of INSTALLED_APPS in setting.py :

Kiểm TRA

We’ll take a test first by adding the following code in users / tests.py and make sure tests fail.

Model Manager

We need the custom Manager by the BaseUserManager subclassing, using an email as the unique identifier instead of the username.

User Model

Select either option using the subclassing AbstractUser or AbstractBaseUser.

AbstractUser

In the above code:

  1. Create a class called CustomUser subclasses AbstractUser
  2. Remove username field
  3. Make the email a require and unique fields
  4. Set the value USERNAME_FIELD – define the unique identifier for the User model as email.
  5. Indicates all objects managed by CustomUserManager.

AbstractBaseUser Update users / models.py

In the above code:

  1. Create a class named CustomUser subclasses AbstractBaseUser
  2. Adds the email, is_staff, is_active, date_joined fields.
  3. Set the value USERNAME_FIELD – define the unique identifier for the User model as email.
  4. Indicates all objects managed by CustomUserManager.

Settings

Add the settings.py line below to let Django know that he will be using the new User class.

AUTH_USER_MODEL = 'users.CustomUser'

Next try applying migrations to create a new database using the custom User model. Before we do this we will use the –dry-run flag to determine what will look like after migrations, with this flag the migration file has not yet been generated.

python manage.py makemigrations --dry-run --verbosity 3

Case customUser subclasses AbstractUser

Case customUser subclasses AbstractBaseUser

Confirm that the migtations does not contain a username. Next create and apply migration.

View schema:

Rerun tests

Forms

Subclass UserCreationForm and UserChangeForm with new CustomUser. Create a new file in users with the name forms.py

Admin

Run the server, login to the admin site. You can add users with email as usual.

Conclusion

In this article we have seen how to custom User model where email is used as primary user identifier instead of username for authentication. Another recommended way to use it is to create a one-to-one model with Django’s User model as Profile model and add new fields under Profile.

Share the news now

Source : Viblo