I’ve been utilizing Django for fairly some time however by no means have I considered this till now.
At the moment, I’ve a undertaking that incorporates totally different consumer ranges. Normally, in my previous expertise, I solely developed methods utilizing Django with solely two consumer ranges that are superuser and regular/common consumer. So my query is what are the efficient methods to current these totally different consumer ranges within the mannequin/database? Right here, I’ll use a college system for instance and likewise present a few of my preliminary ideas on implementing it.
Person ranges:
- Admin (superuser & workers)
- Precept
- Trainer
- College students
Methodology #1: Add new tables based mostly on every consumer stage
from django.contrib.auth.fashions import AbstractUser
from django.db import fashions
class Person(AbstractUser):
consumer = fashions.CharfieldField(max_length = 10, distinctive = True)
class Admin(fashions.Mannequin):
consumer = fashions.OneToOneField(Person, on_delete=fashions.CASCADE, primary_key=True)
class Priciple(fashions.Mannequin):
consumer = fashions.OneToOneField(Person, on_delete=fashions.CASCADE, primary_key=True)
class Trainer(fashions.Mannequin):
consumer = fashions.OneToOneField(Person, on_delete=fashions.CASCADE, primary_key=True)
class Pupil(fashions.Mannequin):
consumer = fashions.OneToOneField(Person, on_delete=fashions.CASCADE, primary_key=True)
Methodology #2: Add extra consumer varieties attributes within the Person mannequin
from django.contrib.auth.fashions import AbstractUser
from django.db import fashions
class Person(AbstractUser):
consumer = fashions.CharfieldField(max_length = 10, distinctive = True)
is_superuser = fashions.BooleanField(default = False)
is_staff = fashions.BooleanField(default = False)
is_principle = fashions.BooleanField(default = False)
is_teacher = fashions.BooleanField(default = False)
is_student = fashions.BooleanField(default = False
'''
Person desk in DB:
consumer | is_superuser | is_staff | is_principle | is_teacher | is_student
'''
My ideas:
In Methodology #1, because the built-in Person mannequin has two related fields, is_staff and is_superuser, Is it potential to implement/change the fields right into a SuperUser/Admin desk as within the instance above? Which means that once I create an admin/superuser, I need it so as to add a brand new row into the Admin desk, as a substitute of including a brand new consumer and updating the consumer’s is_superuser and is_staff fields into 1 within the built-in Person mannequin.
In Methodology #2, the issue with it’s that tables with totally different entry privileges are instantly related to it. For instance, Wage mannequin (which can’t be accessed by Pupil consumer) has a direct hyperlink with the Person mannequin (incorporates Pupil consumer).
I hope I’m able to get some insights and likewise a correct efficient means of implementing this in order that to stop any implementation inconvenience and errors sooner or later. Thanks very a lot.