Sunday, April 26, 2020

Re: Django - What is the best approach to handle multiple user types…and route the HTML pages based on this?

On 27/04/2020 5:04 am, Mayank Tripathi wrote:
>
> I'm making a small test project with below user types: School Admin,
> Teacher, Student, Parent. And each user type will have different
> Permissions like School Admin has full access... Parents can only view
> their Childern's Activity. Teacher can see all students but can add /
> edit marks for their respective students only. Teachers can setup Exam
> etc.. Students can take exam and submit the exam, but cannot edit /
> add any other information. Just can edit his profile detail.
>
> Approach 1: Do i need to create a Custom User calss and apply to each
> user type (ie one Custome Class.. and 4 user type calss).. and
> similarly have 4 different view and html pages?
>
> Approach 2: Just have one custome class and have an field with
> UserType which will have the have as "SchoolAdmin", "Teacher",
> "Student","Parent".. and one view and html (as the data page would
> remain same and only data record will restrict), and somehow identify
> the User Type in View, and filter the record?
>
> Definately some view or html pages will be specific to one user type
> only which is i am able to handle, the issue is to use same view /
> html page to handle all user type.
>
> Please suggest... and any code snippet will will more helpful.
>

I like a single user table (custom user of course) and a single
userprofile table because all users are humans no matter what their role
is. Everyone has the same types of information unique to people.

Then I like to use django.auth.group(s) to allocate roles to people.
That confers great flexibility because you can give anonymous users (no
role) minimal or no access to anything. People can be in one or multiple
roles. For example, a parent can also be a teacher. A student might be a
tutor. A school admin might be a parent.

It then becomes simple to test if someone is in a particular role and
deliver the particular functionality or response needed. You can
decorate views with is_teacher for example.

For this to work nicely, there must be no individual permissions. All
permissions must be assigned to the groups

This approach works for me.

Mike

>
> Below is the thing from approach 1, i was trying.
>
> |#
> models.py---------------------classCustomUser(AbstractUser):USER_TYPE_CHOICES
> =(('SchoolAdmin'),('Teacher'),('Student'),('Parents'),)user_type
> =models.CharField(blank=False,choices=USER_TYPE_CHOICES)name
> =models.CharField(blank=False,max_length=255)country
> =models.CharField(blank=False,max_length=255)city
> =models.CharField(blank=False,max_length=255)phone
> =models.CharField(blank=True,max_length=255)created_at
> =models.DateField(auto_now_add=True)def__str__(self):returnself.name
> classSchoolAdmin(models.Model):user
> =models.OneToOneField(CustomUser,on_delete=models.CASCADE,primary_key=True)classTeacher(models.Model):user
> =models.OneToOneField(CustomUser,on_delete=models.CASCADE,primary_key=True)photo
> =models.ImageField(upload_to='photos/%Y/%m/%d/',blank=True)classStudent(models.Model):user
> =models.OneToOneField(CustomUser,on_delete=models.CASCADE,primary_key=True)teacher
> =models.ForeignKey(Teacher)photo
> =models.ImageField(upload_to='photos/%Y/%m/%d/',blank=True)classParent(models.Model):user
> =models.OneToOneField(CustomUser,on_delete=models.CASCADE,primary_key=True)student=models.ForeignKey(Student)|
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users+unsubscribe@googlegroups.com
> <mailto:django-users+unsubscribe@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/dbc99b6e-3d46-41c4-a5a9-1e85bbf7d8a4%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/dbc99b6e-3d46-41c4-a5a9-1e85bbf7d8a4%40googlegroups.com?utm_medium=email&utm_source=footer>.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/3eb1ccf3-54c9-acca-19bc-e4d38f132a9a%40dewhirst.com.au.

No comments:

Post a Comment