Django Custom User
LINKS
Django Customer User Model
SETUP
To start, create a new Django project from the command line. We need to do several things:
create and navigate into a dedicated directory called accounts for our code install Django make a new Django project called config make a new app accounts start the local web server Here are the commands to run:
$ cd ~/Desktop
$ mkdir accounts && cd accounts
$ pipenv install django~=3.1.0
$ pipenv shell
(accounts) $ django-admin.py startproject config .
(accounts) $ python manage.py startapp accounts
(accounts) $ python manage.py runserver
Note that we did not run migrate to configure our database. It’s important to wait until after we’ve created our new custom user model before doing so.
If you navigate to http://127.0.0.1:8000 you’ll see the Django welcome screen.
Custom user Model
Creating our initial custom user model requires four steps:
- update config/settings.py
- create a new CustomUser model
- create new UserCreation and UserChangeForm
- update the admin
In settings.py we’ll add the accounts app and use the AUTH_USER_MODEL config to tell Django to use our new custom user model in place of the built-in User model. We’ll call our custom user model CustomUser.
Within INSTALLED_APPS add accounts at the bottom. Then at the bottom of the entire file, add the AUTH_USER_MODEL config.
# config/settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'accounts', # new
]
...
AUTH_USER_MODEL = 'accounts.CustomUser' # new
Now update accounts/models.py with a new User model which we’ll call CustomUser.
from django.contrib.auth.models import AbstractUser
from django.db import models
class CustomUser(AbstractUser):
pass
# add additional fields in here
def __str__(self):
return self.username
We need new versions of two form methods that receive heavy use working with users. Stop the local server with Control+c and create a new file in the accounts app called forms.py.
(accounts) $ touch accounts/forms.py