0
0
Djangoframework~30 mins

Custom user model with AbstractUser in Django - Mini Project: Build & Apply

Choose your learning style9 modes available
Custom user model with AbstractUser
📖 Scenario: You are building a Django web application that needs a user model with extra fields beyond the default ones. To do this properly, you will create a custom user model by extending Django's AbstractUser class.
🎯 Goal: Create a custom user model called CustomUser that inherits from AbstractUser and adds a new field called bio to store a short biography. Then configure Django to use this custom user model.
📋 What You'll Learn
Create a new model CustomUser that inherits from AbstractUser
Add a bio field of type TextField to CustomUser
Set the AUTH_USER_MODEL setting to point to CustomUser
Create and apply migrations for the new user model
💡 Why This Matters
🌍 Real World
Many real-world Django applications need to store extra user information beyond the default username and email. Creating a custom user model with AbstractUser is the recommended way to do this cleanly and maintainably.
💼 Career
Understanding how to customize the user model is a common requirement for Django developers working on projects that require user profiles, authentication customization, or additional user data.
Progress0 / 4 steps
1
Create the CustomUser model
In your Django app's models.py, import AbstractUser from django.contrib.auth.models. Then create a class called CustomUser that inherits from AbstractUser. Inside it, add a new field called bio which is a TextField with blank=True.
Django
Need a hint?

Remember to import AbstractUser and models. Define CustomUser as a subclass of AbstractUser. Add bio = models.TextField(blank=True) inside the class.

2
Configure AUTH_USER_MODEL setting
Open your Django project's settings.py file. Add a line that sets AUTH_USER_MODEL to the string "yourapp.CustomUser", replacing yourapp with the actual name of your Django app where CustomUser is defined.
Django
Need a hint?

In settings.py, add the line AUTH_USER_MODEL = "yourapp.CustomUser" with your app name instead of yourapp.

3
Create migrations for the custom user model
Run the Django management command to create migrations for your app. Use the command python manage.py makemigrations yourapp in your terminal, replacing yourapp with your app's name.
Django
Need a hint?

Open your terminal and run python manage.py makemigrations yourapp to create migration files for your custom user model.

4
Apply migrations to update the database
Run the Django management command to apply migrations and update your database schema. Use the command python manage.py migrate in your terminal.
Django
Need a hint?

In your terminal, run python manage.py migrate to apply all migrations and update your database.