← Back to Django Course | Chapter 10: Authentication & Authorization | Lesson 2 of 12

The User Model Overview

The User model is Django's ready-made filing card for every person who can log in to your site.

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.

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

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

markup
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. #}
Common Mistakes
  1. 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.
  2. Confusing username with email — by default Django's User requires a username, not an email, to log in.
  3. Accessing a non-existent field on User (like user.name) instead of the real fields (first_name, last_name).
Chapter Summary
  • 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.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.