Users, Profiles & other things - Part 2

Carrying on from our last post we are going to examine the first option for defining different User types or profiles, which is to add fields to a custom User model. This is the simplest and actually used by Django to define staff and superuser roles.

Typically these fields would either be BooleanField as flags or a CharField with the choices option set. Below is a quick example of how it could work.


class User(AbstractUser):
  # ...
  is_manager = models.BooleanField(default=False)
  is_admin = models.BooleanField(default=False)
  role = models.CharField(max_length=255, choices=(
    ('employee', 'Employee'),
    ('manager', 'Manager'),
    ('admin', 'Administrator'),
  ))

Pros of this approach is that's quick and simple to implement, easy to understand and that queries don't involve any joins to other tables. Some negatives include there is no explicit hierarchy to the roles, any additional roles require code changes and finally it mixes concerns, the User model ought to be purely for authentication, not authorization or permissions.

This approach is certainly one to consider when you need something simple. Tomorrow we will consider using relations.