Extending Django's default User Model
Modifying AUTH_USER_MODEL in Django for a brand new Django project

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 manage.py startup application
echo "
INSTALLED_APPS.append('application',)
AUTH_USER_MODEL = 'application.User'
" >> myPersonalProject/settings.py
echo "
from django.contrib.auth.models import AbstractUser
# from django.contrib.auth.models import Group
class User(AbstractUser):
SUPERADMIN = 0
ADMIN = 1
SALES = 2
MANAGER = 3
ROLE_CHOICES = (
(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/models.py
echo "
from .models import User
admin.site.register([User])
" >> application/admin.py
python manage.py makemigrations
python manage.py migrate
code application/models.py
Uncomment those #s from application/models.py above because groups are created post table creations.
python manage.py migrate
python manage.py createsuperuser
python manage.py 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 :


About the author
- I am a web developer from Navi Mumbai. Mainly dealt with LAMP stack, now into Django and getting into Laravel and Cloud. Founder of nerul.in and gaali.in
Comments
Join the discussionNo comments yet. Be the first to comment.





