Creating a Custom User Model
When building a Django project, there are cases where the default User
model may not provide enough flexibility for your application's requirements. To address this, Django allows you to create a custom User model to suit your specific needs.
Choosing the Base Class
To start, you'll need to decide which base class to use for your custom User model:
AbstractUser
: Extends the default Django User model, allowing you to add custom fields but retains built-in features like authentication and permissions.AbstractBaseUser
: Provides complete control over the User model but requires you to define key fields and methods such as username and password handling.
For most projects, extending AbstractUser
is sufficient, unless you need to overhaul the entire user authentication system.
Creating the Custom User Model
Below is an example of how to create a custom User model by extending AbstractUser
. This will add additional fields to the default User model while retaining its authentication features.
from django.contrib.auth.models import AbstractUser
from django.db import models
class CustomUser(AbstractUser):
phone_number = models.CharField(max_length=15, blank=True)
profile_picture = models.ImageField(upload_to='profile_pics/', null=True, blank=True)
def __str__(self):
return self.username
Registering the Custom User Model
Once you’ve defined your custom User model, you need to register it in your settings.py
file. This tells Django to use the custom User model instead of the default one:
# In settings.py
AUTH_USER_MODEL = 'yourapp.CustomUser'
By setting AUTH_USER_MODEL
, Django will reference your custom User model throughout the project for authentication and user management.
Initial Migration
After defining the custom User model and updating the settings, the next step is to create and apply database migrations:
python manage.py makemigrations
python manage.py migrate
This will create the necessary database tables for the custom User model. Make sure to perform this step at the beginning of the project, as switching to a custom User model after migrations have been applied can be complicated.
Admin Integration
To manage the custom User model through the Django admin interface, you will need to register it in admin.py
:
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import CustomUser
class CustomUserAdmin(UserAdmin):
model = CustomUser
# Optionally, add custom fields to be displayed in admin
fieldsets = UserAdmin.fieldsets + (
(None, {'fields': ('phone_number', 'profile_picture')}),
)
admin.site.register(CustomUser, CustomUserAdmin)
With this setup, your custom User model will be accessible and manageable through the Django admin panel, along with any additional fields you've added.
Next Steps
After creating the custom User model, it's important to ensure the authentication and user management flow is properly set up. In the next section, we'll cover how to handle authentication and superuser creation with the custom User model.