DEV Community

Cover image for How to create custom User model in Django
Kritebh Lagan Bibhakar
Kritebh Lagan Bibhakar

Posted on

How to create custom User model in Django

We can't imagine a web app without a custom User model, so today I will show you how you can create a custom User model in Django which will override the default User model.
Here are the main features of this custom model -

  • Login by email instead of username
  • Add own fields like Date of birth, Address, Id etc.

Let's quickly set up a new Django-Project

python -m venv env
cd env/Scripts
cd ../..
pip install django
django-admin startproject custom_user_model
cd custom_user_model
python startapp custom
Enter fullscreen mode Exit fullscreen mode


Import the necessary modules like AbstractBaseUser, BaseUserManager, PermissionsMixin etc.

from django.contrib.auth.models import AbstractBaseUser,BaseUserManager,PermissionsMixin
from django.utils.translation import gettext_lazy as _
from django.db import models
Enter fullscreen mode Exit fullscreen mode

I am going to create a model first and then its manager.

class Account(AbstractBaseUser,PermissionsMixin):
    email         = models.EmailField(_('email address'),max_length=60,unique=True)
    username      = models.CharField(max_length=30,unique=True)
    first_name    = models.CharField(max_length=30,blank=True)
    last_name     = models.CharField(max_length=30,blank=True)
    date_of_birth = models.DateField(verbose_name='date of birth',null=True,blank=True)
    city          = models.CharField(max_length=50,blank=True)
    date_joined   = models.DateTimeField(verbose_name='date_joined',auto_now_add=True)
    last_login    = models.DateTimeField(verbose_name='last login',auto_now=True)
    is_admin      = models.BooleanField(default=False)
    is_active     = models.BooleanField(default=False)
    is_staff      = models.BooleanField(default=False)
    is_superuser  = models.BooleanField(default=False)

    objects = AccountManager()
    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['username','first_name']

    def __str__(self):
Enter fullscreen mode Exit fullscreen mode

Now create a Manager, here's the code for it

class AccountManager(BaseUserManager):
    def create_user(self,email,username,first_name,password,**other_fields):
        if not email:
            raise ValueError(_("Users must have an email address"))
        if not username:
            raise ValueError(_("Users must have an unique username"))

    def create_superuser(self,email,username,first_name,password,**other_fields):
            if other_fields.get('is_staff') is not True:
                raise ValueError('is_staff is set to False')
            if other_fields.get('is_superuser') is not True:
                raise ValueError('is_superuser is set to False')
            return self.create_user(email,username,first_name,password,**other_fields)
Enter fullscreen mode Exit fullscreen mode

Our Models is ready but for see it we have to customize the file


Import the Account model and all other necessary modules and register the model at

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import Account 
# Register your models here.

class UserAdminConfig(UserAdmin):
    fieldsets = (
        (None, {'fields': ('email', 'password')}),
        ('Personal info', {'fields': ('username','first_name','last_name','date_of_birth','city')}),
        ('Activity', {'fields': ('date_joined','last_login')}),
        ('Permissions', {'fields': ('is_admin','is_active','is_staff','is_superuser')}),
    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('email','first_name', 'date_of_birth','city', 'password1', 'password2'),
Enter fullscreen mode Exit fullscreen mode

So our Custom User Model is ready to use or test.

If you just want to add some extra field to User model then you can just extend the User model by the OneToOne relationship which I will talk in the next post.

Here is the full code at Github, You will find and in a custom folder which we have discussed above.

GitHub logo kritebh / custom-user-model-django

How to Create Custom User Model in Django

Top comments (1)

codebyline profile image
Yumei Leventhal

This helped me finally begin to understand the CustomUser model. I went through Django docs multiple times but couldn't put it all together. So thanks! Followup question: So this CustomUser model be the basis for creating users--Is it a no-no to name the model "User", instead of "Account"?