How to Extend Django User Model in a Custom User Model

Posted By Amit Patel | 29-Nov-2018

Quick Summary:-

In this blog, we will learn how to create a new Django project with a custom User model. This means how to extend Django User model fields in a custom User model.



  1. I have used python version 3.5.2
  2. Django version 2.0.2
  3. Operating System Ubuntu 16.04
  4. mysql
  5. IDE - pycharm-community-2018.2.5

Let's start to create Django project:- 

#  This command will create Django project with demopythonproject name.
         $ startproject demopythonproject

#  View the list of files or folders.
         $ ls

#  Select dictory/folder.
         $ cd demopythonproject


Now, create users app:-

#  In Django, we create app module "users" using this command.

$ python3 startapp users

And run the project-

#  Using this command, we will start the project.

$ python3 runserver

If we navigate this Url We'll see Django welcome page.


Custom User Model:-

We will add "users" app in and use the "AUTH_USER_MODEL" config to tell Django to use our new custom user model in place of Django User model. And our custom user model will CustomUser.

Add "users.apps.UsersConfig" at the bottom of "INSTALLED_APPS". And add the "AUTH_USER_MODEL" config at the bottom of the entire file.

                ## demopythonproject/

                INSTALLED_APPS = [

                AUTH_USER_MODEL = 'users.CustomUser'


Update database configuration in

                ## demopythonproject/

                DATABASES = {
                    'default': {
                        'ENGINE': 'django.db.backends.mysql',
                        'NAME': 'custom',
                        'USER': 'root',
                        'PASSWORD': 'root',
                        'HOST': 'localhost',
                        'PORT': '3306',

where 'custom' is database name. And we will create 'custom' database through terminal.


In, we will add CustomUser model.

CustomUser model, that means it is a entity class. And created "users_customuser" table in "custom" database. Where all entry of CustomUser will be store.

             ## users/

             from django.contrib.auth.models import AbstractUser
             from django.db import models

             # Create your models here.

             class CustomUser(AbstractUser):
                  # add additional fields in here

                  description = models.CharField(max_length=100, default='')
                  city = models.CharField(max_length=100, default='')
                  website = models.URLField(default='')
                  phoneNumber = models.IntegerField(default=0)
                  image = models.ImageField(upload_to='profile_image', blank=True)

                  def __str__(self):

Now we can run makemigrations and migrate commands to create a new database that uses the custom user model.

                 $ python3 makemigrations users
                 $ python3 migrate

                   In this article, we have covered how to create a custom User model to extend Django User model fields.

              External links to the source code-


Request for Proposal

Recaptcha is required.

Sending message..