0
0
DjangoHow-ToBeginner · 4 min read

How to Extend User Model in Django: Simple Guide

To extend the user model in Django, you can either subclass 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.AbstractUser and add extra fields.
  • User Profile Model: Create a separate model with a OneToOneField to the built-in User model to store additional info.

Subclassing requires updating AUTH_USER_MODEL in settings before migrations. The profile model approach works with the default user model.

python
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.

python
# 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()
Output
User object created with username 'alice' and bio 'Hello, I am Alice.'
⚠️

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.

python
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

MethodDescriptionWhen to Use
Subclass AbstractUserCreate a custom user model with extra fieldsWhen you want to change user fields or add new ones directly
User Profile ModelAdd extra info linked to default User via OneToOneFieldWhen you want to keep default user model and add profile data
Set AUTH_USER_MODELUpdate settings to point to custom user modelMust be done before first migration
Use get_user_model()Access user model dynamicallyAlways use in code to support custom user models

Key Takeaways

Set AUTH_USER_MODEL before the first migration when subclassing AbstractUser.
Use a profile model with OneToOneField to extend user data without changing the user model.
Always use get_user_model() to access the user model in your code.
Create user profiles automatically using signals when using a profile model.
Subclassing AbstractUser lets you add fields directly to the user model.