How to Build a Custom User Model in Django Step‑by‑Step

This guide walks you through installing Django, creating an app, defining a custom user model with a manager, configuring AUTH_USER_MODEL, and migrating the database, enabling email‑based authentication and flexible user fields.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
How to Build a Custom User Model in Django Step‑by‑Step

This article helps you create a custom user model in Django.

Why customize the user model?

Having a custom user model is essential for future changes, such as using email instead of username for login, making it a good habit for developers.

Django installation

Create a Django app

Custom model for the app

Migrate the database

1. Django Installation

After setting up Django, run the following command to create a project.

django-admin startproject mysite

2. Create a Django App

Create an app named accounts to manage all account‑related functionality. python manage.py startapp accounts The app contains the following files:

accounts/
    __init__.py
    admin.py
    apps.py
    models.py
    tests.py
    views.py

All database model code should be placed in models.py, and a urls.py file should be added for routing.

3. Custom Model for the App

Define the custom user model and its manager in models.py as shown below.

from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
import uuid

class UserManager(BaseUserManager):
    def create_user(self, email, firstname, lastname, phone, password=None):
        user = self.model(
            email=self.normalize_email(email),
            firstname=firstname,
            lastname=lastname,
            phone=phone,
        )
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, email, firstname, lastname, phone, password=None):
        user = self.create_user(
            email=email,
            password=password,
            firstname=firstname,
            lastname=lastname,
            phone=phone,
        )
        user.is_admin = True
        user.is_staff = True
        user.save(using=self._db)
        return user

class User(AbstractBaseUser):
    id = models.CharField(max_length=200, default=uuid.uuid4, unique=True, primary_key=True)
    email = models.EmailField(max_length=100, unique=True)
    firstname = models.CharField(max_length=100)
    lastname = models.CharField(max_length=100)
    phone = models.IntegerField(unique=True)
    date_joined = models.DateTimeField(auto_now=True)
    last_login = models.DateTimeField(auto_now=True)
    is_admin = models.BooleanField(default=False)
    is_active = models.BooleanField(default=True)
    is_staff = models.BooleanField(default=False)
    is_superuser = models.BooleanField(default=False)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['firstname', 'lastname', 'phone']

    objects = UserManager()

    def __str__(self):
        return self.email + ", " + self.firstname

    def has_perm(self, perm, obj=None):
        return self.is_admin

    def has_module_perms(self, app_label):
        return True

The manager handles creation of regular users and superusers, while the model uses email as the login identifier and includes additional required fields.

After defining the model, add the following setting to settings.py to tell Django to use it.

AUTH_USER_MODEL = 'accounts.User'

4. Migrate the Model

Run the migration commands to apply the custom user model to the database.

python manage.py makemigrations
python manage.py migrate

To migrate only the accounts app, use:

python manage.py makemigrations accounts
python manage.py migrate accounts
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendDjangoAuthenticationdatabase migrationcustom user model
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.