Anjanesh Lekshminarayanan



Extending Django's default User Model

Extending Django's default User Model

Modifying AUTH_USER_MODEL in Django for a brand new Django project

Anjanesh Lekshminarayanan's photo
Anjanesh Lekshminarayanan
·Mar 7, 2023·

2 min read

If you're starting a brand new Django project and want to modify the user model, try this - this is tricky in the sense that it has the been done step-by-step without intervention in-between.

In this case, we're adding a role and date-of-birth (dob) to the user model which will be changed to application_user instead of the default auth_user in the database's table name.

cd ~/Desktop/

mkdir user-roles

cd user-roles

python3 -m venv env

source env/bin/activate

python -m pip install --upgrade pip

pip install django

django-admin startproject myPersonalProject

cd myPersonalProject

python startup application

echo "
AUTH_USER_MODEL = 'application.User'
" >> myPersonalProject/

echo "
from django.contrib.auth.models import AbstractUser
# from django.contrib.auth.models import Group

class User(AbstractUser):
    ADMIN = 1
    SALES = 2
    MANAGER = 3

      (SUPERADMIN, 'Super Admin'),
      (ADMIN, 'Admin'),
      (SALES, 'Sales'),
      (MANAGER, 'Manager'),
    role = models.PositiveSmallIntegerField(choices=ROLE_CHOICES, blank=True, null=True)
    dob = models.DateField(blank=True, null=True)

# hospital_group, created = Group.objects.get_or_create(name='Hospital')
# clinic_group, created = Group.objects.get_or_create(name='Clinic')
# pharmacy_group, created = Group.objects.get_or_create(name='Pharmacy')
" >> application/

echo "
from .models import User[User])
" >> application/

python makemigrations

python migrate

code application/

Uncomment those #s from application/ above because groups are created post table creations.

python migrate

python createsuperuser

python runserver

Goto : http://localhost:8000/admin/

When you try to add a user at http://localhost:8000/admin/application/user/add/ you'll see the following as additional :

Share this