The User Model Overview
In this page:
Default User Fields
The built-in User model has username, email, password, first_name, last_name, is_active, is_staff, is_superuser, and date_joined.
Example: Default User Fields
create_user() is the safe way to make a user — it hashes the password for you instead of storing it raw.
from django.contrib.auth.models import User
user = User.objects.create_user(username='alice', email='[email protected]', password='StrongPass123')
print(user.username)
print(user.email)
print(user.is_active)
⚠️ Run this command in your terminal.
Querying Users
Because User is a normal Django model, you query it with the same ORM methods used for any other model.
Example: Querying Users
filter() and get() work on User exactly like on any custom model, since it is just a model.
python manage.py shell
>>> from django.contrib.auth.models import User
>>> User.objects.filter(is_active=True).count()
>>> User.objects.get(username='alice')
⚠️ Run this command in your terminal.
Attaching Extra Profile Data
Since you shouldn't edit Django's built-in User model, add extra fields with a separate model linked by a OneToOneField.
Note: This keeps Django's internal User model untouched and upgrade-safe.
Example: Attaching Extra Profile Data
OneToOneField links exactly one Profile to exactly one User, so user.profile.bio becomes available.
from django.contrib.auth.models import User
from django.db import models
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
bio = models.TextField(blank=True)
def __str__(self):
return self.user.username
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
- Adding extra fields (like bio or avatar) directly onto Django's built-in User model instead of using a related profile model or a custom user model.
- Confusing username with email — by default Django's User requires a username, not an email, to log in.
- Accessing a non-existent field on User (like user.name) instead of the real fields (first_name, last_name).
- django.contrib.auth.models.User is Django's built-in model for user accounts.
- It ships with fields like username, email, password, first_name, last_name, and is_active.
- Passwords are never stored in plain text — Django hashes them automatically.
- For extra fields, attach a separate profile model rather than editing User directly.
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first:
- Introduction to Django's Auth System
- The User Model Overview
- Setting Up a Login View
- Logout Functionality
- User Registration Form
- The login_required Decorator
- The permission_required Decorator
- Django Groups and Permissions
- Password Hashing in Django
- Session Authentication Basics
- Introduction to Custom User Models
- Sending Emails with Django