0
0
Djangoframework~8 mins

Custom user model with AbstractUser in Django - Performance & Optimization

Choose your learning style9 modes available
Performance: Custom user model with AbstractUser
MEDIUM IMPACT
This affects the initial page load speed and authentication-related interactions by changing how user data is loaded and managed.
Extending user data with a custom user model
Django
from django.contrib.auth.models import AbstractUser
from django.db import models

class CustomUser(AbstractUser):
    extra_field = models.CharField(max_length=100)

# Set AUTH_USER_MODEL = 'app.CustomUser' in settings.py
Extending AbstractUser keeps all user data in one table, reducing joins and speeding up queries.
📈 Performance GainSingle database query per user load, reducing authentication latency by up to 50ms
Extending user data with a custom user model
Django
from django.contrib.auth.models import User
from django.db import models

# Adding profile info via separate model with OneToOneField
class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    extra_field = models.CharField(max_length=100)
This pattern causes extra database joins on every user query, increasing query time and slowing authentication-related views.
📉 Performance CostTriggers multiple database queries per user load, increasing response time by 50-100ms depending on DB size
Performance Comparison
PatternDatabase QueriesJoinsQuery LatencyVerdict
Separate Profile Model2+ queries per user1 join per userHigher latency due to joins[X] Bad
CustomUser extends AbstractUser1 query per userNo joins neededLower latency, faster auth[OK] Good
Rendering Pipeline
Custom user models affect server-side data fetching and authentication logic, which impacts how fast user data is ready for rendering.
Data Fetching
Server Response
Client Rendering
⚠️ BottleneckDatabase query time during authentication and user data retrieval
Core Web Vital Affected
INP
This affects the initial page load speed and authentication-related interactions by changing how user data is loaded and managed.
Optimization Tips
1Use AbstractUser to keep user data in one table and avoid joins.
2Add database indexes on custom user fields to speed queries.
3Avoid separate profile models that cause extra database joins during authentication.
Performance Quiz - 3 Questions
Test your performance knowledge
Why is extending AbstractUser better for performance than using a separate profile model?
AIt automatically caches user data in the browser.
BIt reduces the number of database queries by storing all user data in one table.
CIt eliminates the need for authentication.
DIt uses less memory on the client side.
DevTools: Network
How to check: Open DevTools, go to Network tab, filter for authentication API calls, and inspect timing details for user data requests.
What to look for: Look for multiple sequential requests or long response times indicating extra queries or joins slowing down user data loading.