Django for Beginners - Custom User Model

In progress Django

Source: Django for Beginners , William S. Vincent

Key ideas

  • Start every new project with a custom user model, even one that adds nothing yet: replacing Django's built-in User later is painful.
  • Subclass AbstractUser rather than AbstractBaseUser: you keep everything the built-in user has and can still add fields.
  • The chapter starts a Newspaper app properly, with environment variables and a custom user from day one (a nod to Django's origins at the Lawrence Journal-World newspaper).
  • The example adds an optional age field with both null=True and blank=True, which raises the difference between the two options.

In my own words

The user model is the model everything else points at: admin log entries, groups, permissions, and every ForeignKey to an author or owner. Choosing it is a schema decision taken at the first migration, so the cheap move is to make it swappable before anything depends on it (see Django Custom User Model Must Exist Before the First Migration).

null and blank look alike but act on different layers: one on the database column, the other on form validation. For text fields the convention is to store "" rather than NULL, so "no data" has a single spelling (see Django null Is a Database Setting, blank Is a Validation Setting).

Questions

  • Why AbstractUser and not AbstractBaseUser? AbstractUser is the full built-in user (username, names, email, staff and permission flags) as an abstract model. AbstractBaseUser only provides password handling and last_login, so you write the manager, USERNAME_FIELD and permissions yourself. It is worth it only when sign-in really differs, such as email-only login.
  • What goes wrong with CharField(null=True, blank=True)? The column accepts both NULL and "", so "empty" has two values and every query has to check for both.
  • Open: when users sign in with their email, is it simpler to keep AbstractUser and make email unique, or to move to AbstractBaseUser and drop username?

References

Review cards

Flashcard Why start every Django project with a custom user model, even an empty one?

Everything points at the user model (foreign keys, admin logs, groups, permissions), and it is fixed at the first migration. Swapping it later means rewriting migrations and data; a custom model from day one costs nothing and leaves room to add fields.

Flashcard In Django, what does `null=True` act on, and what does `blank=True` act on?

null=True acts on the database column (it may store NULL); blank=True acts on validation (forms and full_clean() accept an empty value).

Quiz What is the difference between `AbstractUser` and `AbstractBaseUser`?
  • AbstractUser is the complete built-in user as an abstract model; AbstractBaseUser gives only password handling and last_login
  • AbstractBaseUser is the complete built-in user; AbstractUser gives only password handling
  • They are interchangeable aliases of the same class
  • AbstractUser is for username login, AbstractBaseUser for social login

With AbstractBaseUser you write the manager, USERNAME_FIELD and permissions yourself; it is worth it only when sign-in really differs, such as email-only login.

Flashcard What goes wrong with `CharField(null=True, blank=True)`?

"Empty" can then be stored two ways, NULL and "", so every query has to check for both. Text fields should use blank=True alone and store "".