Django Extend User Model with One-to-One Field

Add custom user field that related to User Model.

Photo by Florian Olivo on Unsplash

Step 1: Preparation, Create Django Project, Initial Migration
create virtualenv: virtualenv venv
start virtualenv: venv/Scripts/activate
install Django in virtualenv: pip install django==3.0
Create Django: django-admin startproject myproject
Go to myproject folder: cd myproject
Initial Migration: python manage.py migrate

Step 2: Create Django Apps
Create apps: python manage.py startapp myapp
Add myapp to INSTALLED_APPS in myproject/settings.py

Step 3: Add Profile Class in myapp/models.py
Add Profile class that extend built-in User Model

myapp/models.py

Step 4: Makemigrations and Migrate
Make migrations: python manage.py makemigrations
Migrate: python manage.py migrate

Step 5: Add Profile in Admin Site
Update myapp/admin.py
This is for testing purpose in django admin site

myapp/admin.py

Step 6: Testing in Django Admin Site
Create super user: python manage.py createsuperuser

Try to access: http://127.0.0.1:8000/admin

Try to login in Django admin with superuser

Try to update user profiles in Profiles menu

Important Note: Rendering in HTML
For further development, we can use this html code to show user and its profile in one HTML page.

Software Engineer