Handling Multiple Users in Django
Handling Users is a very common issue when you’re writing your Django application, especially when you’re a beginner, you should be asking yourself various but standard questions.
I will try to give you some basics of User handling in Django, so that you will have the ability to manage your Users with much more flexibility.
Built in User model: (Three User types)
In the base user model, Django provides us with three fields (BooleanFields) that help us ditinguish between our users. A standard case is to have three types of users: an admin/superuser User and who will have the
is_superuser() flag set to True, a staff User which will have the
is_staff() flag set to True, and a Normal user which will have an is_acive() flag set to True.
The first two user types(superuser and staff) have access to the admin page, but the last one can't. We can use that to our advantage in some way.
The built-in registration form saves Users to the database with neither
is_staff() flags set to True, but with the is_active set to True.
When creating your software, you have to understand its behaviors and know how much Users you want to implement, after that, if two Users are enough, use the standard User built-in with Django, if that does not seem to fulfill your request, please keep up with me.
Multiple User types (Extending the base User model):
If you’ve made it this far, it’s that you have a much more complicated project to build. In this case, if you want your Users to have one or two roles at the same time (example: teacher and student), you can create flags into the base User model. We can do that by customizing the default User model:
from django.contrib.auth.models import AbstractUser class User(AbstractUser): is_type1 = models.BooleanField('type1', default=False) is_type2 = models.BooleanField('type2', default=False)
We're here telling Django that we would like to extend the base user model with some new fields. That is to have more control over our base user(or for whatever other reason). Generally, when creating an app, it is a good practice before running the migrations, to create a User class that extends the base user model, and keep it empty, so as not to complicate your life later on, when trying to create one. Because extending a user model down the line can cause serious problems to the database.
from django.contrib.auth.models import AbstractUser class User(AbstractUser): pass
AUTH_USER_MODEL = 'appname.User'
Now that we have modified the User table, we should add
AUTH_USER_MODEL = ‘appname.User’ to our *settings.py* file, then run the migrations commands.
In this case you will have multiple types of users, the Admin User, the type1 User with the
is_type1() flag and the type2 user with the
You can now control your User's restrictions freely and with more flexibility.
I have created a Django project as a demonstration of this part, check it out here.
Multiple User types (A one to one relationship):
When your idea requiers multiple Users, you must in the first place decide if you want your Users to have multiple types at the same time, if it’s the case, create a User table and link it to the main User table with a one to one relationship, this will give us the possibility to have multiple custom user tables tied up to the base user model.
class Teacher(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) #Other fields here... def __str__(self): return self.user.username
This was an overview about how you can manage Users when working on a Django project, the first idea is to use the built in Django User model. The second is when you have many User types you should customize the User model by adding custom flags/BoleanFields. And the third is when you also have multiple User types, in this one, you can create the desired user table and link it with a one-to-one relationship to the main User class.