Django Custom User Model Must Exist Before the First Migration

In progress Django

Django lets you replace its built-in User with your own model through the AUTH_USER_MODEL setting, but the switch is only cheap before the first migrate. Once tables exist, Django's own apps and yours hold foreign keys to auth_user, and moving them to another table means fixing the schema by hand. A custom user that adds nothing on day one costs one small app; adding it later costs a migration project.

Why the Switch Gets Hard Later

The first migrate creates auth_user and every table that references it: django_admin_log, the user–group and user–permission join tables, and any ForeignKey to the user in your own apps. The migration framework can't retarget those relations to a new table on its own, and the custom model has to be created in the first migration of its app. Django's documentation describes the mid-project change as significantly more difficult and leaves the schema work to you.

The Minimal Pattern

# apps/accounts/models.py
from django.contrib.auth.models import AbstractUser

class CustomUser(AbstractUser):
    pass  # add fields later with ordinary migrations, e.g. age

# settings.py
AUTH_USER_MODEL = "accounts.CustomUser"

Subclassing AbstractUser keeps everything the built-in user has (username, names, email, staff and permission flags). AbstractBaseUser keeps only password hashing and last_login: it suits a login model that really differs, such as email-only sign-in, and needs your own manager and USERNAME_FIELD.

Referring to the User Model

Once the model is swappable, never import User directly:

  • in model fields, point at the setting: models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE);
  • at runtime (views, the shell, tests), call get_user_model().

Code written this way works with whatever AUTH_USER_MODEL names, which also keeps reusable apps portable between projects.

Don't Override groups and user_permissions

Some tutorials redefine groups and user_permissions on the custom user with new related_name values to avoid the fields.E304 reverse accessor clash. The clash only happens when both auth.User and the custom model are active, that is when AUTH_USER_MODEL is missing. With the setting in place, auth.User is swapped out and the fields inherited from PermissionsMixin work as they are.

New Fields and createsuperuser

createsuperuser only prompts for USERNAME_FIELD, the password and the fields listed in REQUIRED_FIELDS. A new field that is neither optional nor listed makes the command fail when it saves the row. Make such fields optional (null=True, blank=True for an age, see Django null Is a Database Setting, blank Is a Validation Setting) or add them to REQUIRED_FIELDS.

References

Review cards

Flashcard Why is switching to a custom user model hard after the first migrate?

auth_user then exists, and Django's tables and yours hold foreign keys to it. The migration framework can't retarget them, so the switch means fixing the schema by hand.

Flashcard How should you refer to the user model in your code after it has been set as swappable?

You should use settings.AUTH_USER_MODEL in model fields and get_user_model() at runtime to ensure compatibility with the custom user model.

Flashcard What is the consequence of adding new fields to the custom user model that are neither optional nor listed in `REQUIRED_FIELDS`?

When using createsuperuser, such fields will cause the command to fail because they are not included in the fields that are prompted for.

Quiz What should you do if you encounter `fields.E304` reverse accessor clash error?
  • Rename the reverse accessor in the custom user model.
  • Set AUTH_USER_MODEL to the custom user model name.
  • Ignore the error and proceed with the migration.
  • Modify the related_name in the groups and user_permissions fields.

Setting AUTH_USER_MODEL ensures that the built-in User model is swapped out, resolving the clash.