How to Extend User Model in Django: Simple Guide
AbstractUser to add fields directly or create a separate profile model linked with a OneToOneField. The first method customizes the user model itself, while the second adds extra info without changing the original user model.Syntax
There are two main ways to extend the Django user model:
- Subclass AbstractUser: Create a new user model by inheriting from
django.contrib.auth.models.AbstractUserand add extra fields. - User Profile Model: Create a separate model with a
OneToOneFieldto the built-inUsermodel to store additional info.
Subclassing requires updating AUTH_USER_MODEL in settings before migrations. The profile model approach works with the default user model.
from django.contrib.auth.models import AbstractUser from django.db import models # Subclass AbstractUser example class CustomUser(AbstractUser): bio = models.TextField(blank=True) # User Profile model example from django.contrib.auth.models import User class Profile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) bio = models.TextField(blank=True)
Example
This example shows how to create a custom user model by subclassing AbstractUser and adding a bio field. It also shows how to update settings.py to use the new model.
# models.py from django.contrib.auth.models import AbstractUser from django.db import models class CustomUser(AbstractUser): bio = models.TextField(blank=True) # settings.py AUTH_USER_MODEL = 'myapp.CustomUser' # Usage in views or admin from django.contrib.auth import get_user_model User = get_user_model() # Create a user with bio user = User.objects.create_user(username='alice', password='pass123') user.bio = 'Hello, I am Alice.' user.save()
Common Pitfalls
Changing AUTH_USER_MODEL after migrations: You must set AUTH_USER_MODEL before the first migration. Changing it later causes errors.
Forgetting to create a profile on user creation: When using a profile model, you must create the profile instance manually or use signals.
Not using get_user_model(): Always use get_user_model() instead of importing User directly to support custom user models.
from django.db import models from django.db.models.signals import post_save from django.dispatch import receiver from django.contrib.auth import get_user_model User = get_user_model() class Profile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) bio = models.TextField(blank=True) @receiver(post_save, sender=User) def create_user_profile(sender, instance, created, **kwargs): if created: Profile.objects.create(user=instance) @receiver(post_save, sender=User) def save_user_profile(sender, instance, **kwargs): instance.profile.save()
Quick Reference
| Method | Description | When to Use |
|---|---|---|
| Subclass AbstractUser | Create a custom user model with extra fields | When you want to change user fields or add new ones directly |
| User Profile Model | Add extra info linked to default User via OneToOneField | When you want to keep default user model and add profile data |
| Set AUTH_USER_MODEL | Update settings to point to custom user model | Must be done before first migration |
| Use get_user_model() | Access user model dynamically | Always use in code to support custom user models |