I am creating a Django site that will have 4 types of users;
Noting that I plan on creating a custom interface and not utilizing the built-in admin interface. What would be the best method for implementing this user structure? I would prefer to use Django's built-in authentication system (which manages password salting and hashing, along with session management etc), but I would be open to using some other authentication system if it is secure and production ready.
EDIT: Also, my plan is to use 1 log-in screen to access the site and utilize the permissions and user type to determine what will be available on each user's dashboard.
EDIT: Would it be possible to accomplish this by using an app for each user type, and if so how would this be done with a single log-in screen.
First of all, you cannot make multiple authentication user base for a project. So you have to use the Django user authentication provided and fork it for multiple types of users. The Django user has some default values you need to provide during registration (try creating a user in Django Admin). What you can do is create a model called 'CustomUser' and inherit from AbstractUser
. This will make your 'CustomUser' model the default for the project users. Because you inherit from AbstractUser
this 'CustomUser' model will have every field from the original Users model, and then you can add some field on your own. You also need to specify in the settings.py
file of the project that the original Users model is not your default authentication model anymore, it is your new 'CustomUser' model that will be used for authentication. See if the following code helps.
from django.db import models from django.contrib.auth.models import AbstractUser class CustomUser(AbstractUser): type_choices = ( ('SU', 'Super User'), ('A', 'User Type A'), ('B', 'User Type B'), ('C', 'User Type C'), ) user_type = models.CharField(max_length=2, choices=type_choices, default='C') class UserDetails(model.Model): type = models.OneToOneField('CustomUser') extra_info = models.CharField(max_length=200)
In the above code you have created the CustomUser model where users can provide the basic info like username, password, etc. which is the default in Django. And then you select which user type it is and save your additional information on UserDetails model which also has OneToOne relation to your new authentication model. One last thing you need to do is in the settings.py
file.
AUTH_USER_MODEL = 'index.CustomUser'
Over here index
is the app that my CustomUser
model is created in.
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With