Set up a Custom User Model in Django.
This article is part of the Build Django app on Docker and deploy it to AWS Fargate (https://qiita.com/keita_gawahara/items/66e58a6eed5c0c5f31d2).
By default, Django has a user model set. However, when you actually create an app, there will always be a time when you want to change the user model to match the app you created. It is very difficult to modify the user model after coding to some extent, so it is recommended to set the Custom User Model from the beginning of the project.
To implement the Custom User Model, use the "AbstractUser" class or the "AbstractBaseUser" class. The "AbstractUser" class is a simple way to extend the default user fields and authentication. The method of using the "AbstractBaseUser" class increases the amount of coding, but it can be customized in more detail.
This time, we will implement it using "Abstract User". I also used "Abstract User" to customize the user model in the project I was involved in developing. For general user management, "Abstract User" is sufficient.
Implement the Custom User Model in the project created with Using PostgreSQL with Docker + Django.
First, create a users app that manages custom users with the startapp command. Then add the Custom User Model related code to setting.py.
docker-compose exec web python manage.py startapp users
Let's add the created users to setting.py. Also, at the end, state that the custom user model will be used instead of the default user model.
setting.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# Local
'users.apps.UsersConfig', #add to
]
...
AUTH_USER_MODEL = 'users.CustomUser' #add to
Create a CustomUser class in models.py. This time, we will not add fields from the default user model, so add pass directly under it. If you want to set your own field, please add the field here.
users/models.py
from django.contrib.auth.models import AbstractUser
from django.db import models
class CustomUser(AbstractUser):
pass
Create users / forms.py and add the following code.
users/form.py
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
class CustomUserCreationForm(UserCreationForm):
class Meta(UserCreationForm):
model = get_user_model()
fields = ('email', 'username',)
class CustomUserChangeForm(UserChangeForm):
class Meta(UserChangeForm):
model = get_user_model()
fields = ('email', 'username',)
Add the following code to admin.py.
users/admin.py
from django.contrib import admin
from django.contrib.auth import get_user_model
from django.contrib.auth.admin import UserAdmin
from .forms import CustomUserCreationForm, CustomUserChangeForm
CustomUser = get_user_model()
class CustomUserAdmin(UserAdmin):
add_form = CustomUserCreationForm
form = CustomUserChangeForm
model = CustomUser
list_display = ['email', 'username',]
admin.site.register(CustomUser, CustomUserAdmin)
This completes the settings. Finally, let's migrate.
docker-compose exec web python manage.py makemigrations users
docker-compose exec web python manage.py migrate
Recommended Posts