22👍
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.
0👍
You should need just use Groups Django mechanism – you need to create four groups like userA and let say common and check whether user is in first or second group – then show him appropriate view
- [Django]-Detect mobile browser (not just iPhone) in python view
- [Django]-Python/Django: How to remove extra white spaces & tabs from a string?
- [Django]-Loading initial data with Django 1.7+ and data migrations