liveBook · Manning

Custom user model

As explained in Django in Action

A custom user model is an application-defined model that replaces Django's built-in user model and can define different account data and credential behavior.

A custom user model replaces Django’s built-in User model when an application needs different account data or credential behavior. For example, it can use an email address instead of a username for login. section 6.2.1

Choose the account-storage approach early:

  • Use a profile model when the built-in User credentials are sufficient. Connect the profile to User with a OneToOneField, which permits one profile per account and one account per profile. section 6.2.1
  • Replace User with a custom model when account data or credential behavior must change. Replacing it also requires replacing the associated user manager. section 6.2.1
  • Treat the choice as an early design decision: Django uses one account mechanism at a time, and changing mechanisms later can require custom migrations and data cleanup. section 6.2.1

Email-based login

A musician account may need an email address as its login credential rather than a username. A profile model cannot change the built-in credential mechanism; a custom user model can. section 6.2.1

Figure 6.2 User profiles vs. replacing the User ORM object for user data storage
figure

Compare the objects and database relationships used by a profile model and a replacement User model. section 6.2.1

How Django in Action covers it

How deep it goes
Explains relationship constraints, credential changes, manager replacement, and migration consequences without implementation code.
Examples
Uses musician accounts, profile objects, and email addresses replacing usernames as concrete account-design examples.
What it assumes
Assumes familiarity with Django User accounts and ORM models while introducing profiles, OneToOneField relationships, and user managers.
What you can do after
Evaluate whether an application needs a profile model or a custom user model before its account data becomes difficult to migrate.
How it connects
Connects custom user models with profiles, OneToOneField relationships, user managers, credentials, authentication, and migrations.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Django in Action. The section links point to the parts of the book it draws on.