Django 1.8, Multiple Custom User Types
Asked Answered
B

2

22

I am creating a Django site that will have 4 types of users;

  • Super Admin: Essentially manages all types of users
  • UserTypeA: Can login to site and basically will have a CRUD interface for some arbitrary model. Also has extra attributes (specific info that only pertains to TypeA users)
  • UserTypeB: Can login to site and has extra information that pertains specifically to TypeB users, also can create and manage TypeC user accounts
  • UserTypeC: Can login to site and has extra information that pertains only to TypeC 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.

Bulgarian answered 28/5, 2015 at 1:59 Comment(2)
Did you find a solution for this? You could use groups for the various UserTypes that you need to handle and then assign people to the correct group based on some value at the time of registration.Indigent
why do not you use build-in permission/groups of django.Frasco
C
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.

Corker answered 28/6, 2015 at 18:3 Comment(4)
If the different user types require collection of varying data fields, how is that specified through this method?Hm
Fields which are not unique go in CustomUser class, fields which are common in all users go in UserDetails. Also, it's not a ForeignKey, should be a OneToOneField.Corker
I have similar requirements , I have to manage Multiple use types, 1. Talent - can be actor, dancer, singer etc 2. Maker - can be director, cast director, choreographer, music director, etc How do i maintain different so many types of users?Melanoid
You can create a subtype field in UserDetails model, or you can create all the model types separately. i.e. Types can be Talent-actor, Talent-dancer, Talent-singer, Maker-director, Maker-cast-director, Maker-choreographer, Maker-music-director, etc. Please read Chapter 3 which is on Models from the book Django Design Patterns and Best Practices. Best book for beginners to intermediate coders ever!Corker
O
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

Oddment answered 24/3, 2020 at 13:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.