Django for Beginners - Custom User Model
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
Userlater is painful. - Subclass
AbstractUserrather thanAbstractBaseUser: 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
agefield with bothnull=Trueandblank=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
AbstractUserand notAbstractBaseUser?AbstractUseris the full built-in user (username, names, email, staff and permission flags) as an abstract model.AbstractBaseUseronly provides password handling andlast_login, so you write the manager,USERNAME_FIELDand 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 bothNULLand"", 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
AbstractUserand makeemailunique, or to move toAbstractBaseUserand dropusername?
References
- Django for Beginners, William S. Vincent - the book; the chapter that introduces the custom user model with the Newspaper app.
- Django Custom User Model Must Exist Before the First Migration - atomic note distilled from this chapter.
- Django null Is a Database Setting, blank Is a Validation Setting - atomic note distilled from this chapter.
- Django MOC - hub for Django notes.
- Django documentation: Customizing authentication - the official recommendation the book builds on.
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`?
-
AbstractUseris the complete built-in user as an abstract model;AbstractBaseUsergives only password handling andlast_login -
AbstractBaseUseris the complete built-in user;AbstractUsergives only password handling - They are interchangeable aliases of the same class
-
AbstractUseris for username login,AbstractBaseUserfor 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 "".