DEV Community

willp11
willp11

Posted on

Django Part 2 - Creating a custom User model

We are going to create a custom user class that will be used when users register an account and login. It is much easier to do this before running the initial database migrations, we can then easily make adjustments to the model later.

We will define the custom user model in the users/models.py file. The model will have all the default User model fields, such as username, email and password. On top of that, we are adding an optional name field and an email_verified field that will default to False and update to True when a user verifies their email.

from django.db import models
from django.contrib.auth.models import AbstractUser

class CustomUser(AbstractUser):
    name = models.CharField(max_length=100, null=True, blank=True)
    email_verified = models.BooleanField(default=False)
Enter fullscreen mode Exit fullscreen mode

We need to update the settings.py file so that we can use the newly created CustomUser model for authentication.

AUTH_USER_MODEL = 'users.CustomUser'
Enter fullscreen mode Exit fullscreen mode

Before we can start the server again, we need to run the database migrations. We use the manage.py script to create and run database migrations.

py manage.py makemigrations
py manage.py migrate
Enter fullscreen mode Exit fullscreen mode

Django comes with some pre-made admin functionality that can be accessed at http://127.0.0.1:8000/admin

In order to log in there, we need to create a super user account which can be done using the manage.py script.

py manage.py createsuperuser
Enter fullscreen mode Exit fullscreen mode

Follow the instructions and enter your username, email and password, then navigate to the admin site at http://127.0.0.1:8000/admin

You can now login using the credentials you just created.
In the admin site, we can see all the database models and instances.
Normally, we would be able to see the User model, however at the moment we can only see the Groups model because we created our own custom user model. In order to see the User model in the admin site, we are going to write a custom admin class.

Before we do that, we are going to create some forms that will be used by the admin class to enable us to create and update User instances.
Create a new file, forms.py, inside the users directory.
In users/forms.py:

from django.contrib.auth.forms import UserChangeForm, UserCreationForm
from .models import CustomUser

class CustomUserCreationForm(UserCreationForm):
    class meta(UserCreationForm):
        model = CustomUser
        fields = UserCreationForm.Meta.fields + ('name',)

class CustomUserChangeForm(UserChangeForm):
    class meta(UserChangeForm):
        model = CustomUser
        fields = UserChangeForm.Meta.fields
Enter fullscreen mode Exit fullscreen mode

Then in the users/admin.py file, we will create the custom admin class and register it to the admin site.

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import CustomUser
from .forms import CustomUserChangeForm, CustomUserCreationForm

class CustomUserAdmin(UserAdmin):
    add_form = CustomUserCreationForm
    form = CustomUserChangeForm
    model = CustomUser
    list_display = [
        'email',
        'username',
        'name',
        'is_staff',
        'email_verified'
    ]
    fieldsets = UserAdmin.fieldsets + ((None, {"fields": ("name",)}),) 
    add_fieldsets = UserAdmin.add_fieldsets + ((None, {"fields": ("name",)}),)

admin.site.register(CustomUser, CustomUserAdmin)
Enter fullscreen mode Exit fullscreen mode

Now when logged into the admin site you can see the Users model and our user instance that we are logged in with.

In the next part of the tutorial, we will be creating the API endpoints to register, login and verify email address.

Top comments (0)