0
0
Djangoframework~10 mins

Custom user model with AbstractUser in Django - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Custom user model with AbstractUser
Define CustomUser class
Inherit AbstractUser
Add custom fields/methods
Update settings.py: AUTH_USER_MODEL
Run migrations
Use CustomUser in app
Authentication and user management with new model
This flow shows how to create a custom user model by extending AbstractUser, update settings, migrate, and then use the new user model.
Execution Sample
Django
from django.contrib.auth.models import AbstractUser
from django.db import models

class CustomUser(AbstractUser):
    age = models.PositiveIntegerField(null=True, blank=True)

# settings.py
AUTH_USER_MODEL = 'myapp.CustomUser'
Defines a CustomUser model extending AbstractUser with an extra 'age' field and sets it as the user model in settings.
Execution Table
StepActionCode/SettingResult/Effect
1Define CustomUser classclass CustomUser(AbstractUser): age = models.PositiveIntegerField(null=True, blank=True)CustomUser model created with all AbstractUser fields plus 'age'
2Update settings.pyAUTH_USER_MODEL = 'myapp.CustomUser'Django uses CustomUser as the user model
3Run makemigrationspython manage.py makemigrationsMigration file created for CustomUser model
4Run migratepython manage.py migrateDatabase tables updated to include CustomUser
5Use CustomUser in appfrom django.contrib.auth import get_user_modelCustomUser is used for authentication and user management
6ExitN/ASetup complete, app uses CustomUser
💡 Setup stops after migrations and app uses CustomUser for authentication
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
CustomUserNot definedDefined with AbstractUser + ageSameSameSameSame
AUTH_USER_MODELDefault 'auth.User'Default'myapp.CustomUser''myapp.CustomUser''myapp.CustomUser''myapp.CustomUser'
Migration filesNoneNoneNoneNew migration createdApplied to DBApplied to DB
Key Moments - 3 Insights
Why must AUTH_USER_MODEL be set before the first migrate?
Because Django creates the user table on first migrate; changing AUTH_USER_MODEL later causes conflicts. See execution_table steps 2-4.
Can I add fields directly to AbstractUser?
No, you create a subclass (CustomUser) to add fields. AbstractUser is a base class. See step 1 in execution_table.
How do I reference the custom user model in code?
Use get_user_model() to get the current user model, ensuring compatibility. See step 5 in execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what happens at step 3?
ARun migrate to apply database changes
BDefine the CustomUser class
CCreate migration files for CustomUser
DUpdate settings.py to set AUTH_USER_MODEL
💡 Hint
Check the 'Action' and 'Code/Setting' columns at step 3 in execution_table
At which step does Django start using the CustomUser model for authentication?
AStep 2
BStep 5
CStep 4
DStep 1
💡 Hint
Look for when the app imports and uses the user model in execution_table
If you forget to set AUTH_USER_MODEL before migrating, what is the likely result?
ADjango uses the default User model and migrations conflict later
BCustomUser is used anyway without issues
CMigrations run faster
DDjango raises an error immediately
💡 Hint
Refer to key_moments about setting AUTH_USER_MODEL before first migrate
Concept Snapshot
Custom user model with AbstractUser:
- Create a class inheriting AbstractUser
- Add custom fields as needed
- Set AUTH_USER_MODEL in settings.py
- Run makemigrations and migrate
- Use get_user_model() to access the user model
- Must set AUTH_USER_MODEL before first migrate
Full Transcript
To create a custom user model in Django, define a class that inherits from AbstractUser and add any extra fields you want. Then, update your settings.py file to set AUTH_USER_MODEL to point to your new model. After that, run makemigrations and migrate to create the necessary database tables. Finally, use get_user_model() in your code to reference the custom user model. Remember, you must set AUTH_USER_MODEL before running your first migrate to avoid conflicts.