简体   繁体   English

Django - 处理多种用户类型的最佳方法是什么……并基于此路由 HTML 页面?

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

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?方法 1:我是否需要创建自定义用户 calss 并应用于每种用户类型(即一个 Custome Class.. 和 4 个用户类型 calss).. 并且同样有 4 个不同的视图和 html 页面?

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?方法 2:只有一个客户 class 并有一个 UserType 字段,该字段将具有“SchoolAdmin”、“Teacher”、“Student”、“Parent”.. 和一个视图和 html(因为数据页面将保持不变并且只有数据记录会限制),并以某种方式识别视图中的用户类型,并过滤记录?

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.当然,某些视图或 html 页面将特定于我能够处理的一种用户类型,问题是使用相同的视图/html 页面来处理所有用户类型。

Please suggest... and any code snippet will will more helpful.请提出建议......任何代码片段都会更有帮助。

# models.py
---------------------
class CustomUser(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):
        return self.name


class SchoolAdmin(models.Model):
    user = models.OneToOneField(
        CustomUser, on_delete=models.CASCADE, primary_key=True)


class Teacher(models.Model):
    user = models.OneToOneField(
        CustomUser, on_delete=models.CASCADE, primary_key=True)
    photo = models.ImageField(upload_to='photos/%Y/%m/%d/', blank=True)


class Student(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)

class Parent(models.Model):
    user = models.OneToOneField(
        CustomUser, on_delete=models.CASCADE, primary_key=True)
    student= models.ForeignKey(Student)

Your CustomUser class is essentially correct.您的CustomUser class 基本上是正确的。 You don't really need the other classes (SchoolAdmin, Teacher, Student, Parent) as that functionality is described by user_type .您实际上并不需要其他类(SchoolAdmin、Teacher、Student、Parent),因为该功能由user_type描述。

You do need to change the user type choices, something like:您确实需要更改用户类型选择,例如:

SCHOOL, TEACHER, STUDENT, PARENT = 'school', 'teacher', 'student', 'parent'
USER_TYPES: (
    (SCHOOL, 'School'),
    (TEACHER, 'Teacher'),
    (STUDENT, 'Student'),
    (PARENT, 'Parent'),
)

The photo field can be added to the CustomUser class. photo字段可以添加到CustomUser class。

Furthermore, you will want to familiarize yourself with Django's documentation on custom authentication and permissions and authorization .此外,您还需要熟悉 Django 关于 自定义身份验证权限和授权的文档。

You can then create groups and permissions and assign your users to those.然后,您可以创建组和权限并将您的用户分配给它们。 Teachers are a group with specific permissions, etc.教师是具有特定权限等的组。

Also you can differentiate by user_type in your templates and views, to show or hide information, or access.您还可以通过模板和视图中的 user_type 进行区分,以显示或隐藏信息或访问。 For example:例如:

def exam_setup_view(request):
    # only teachers allowed here, others see 404
    if request.user.user_type != CustomUser.TEACHER:
        raise Http404()

EDIT This article explains how to use Django's groups and permissions.编辑这篇文章解释了如何使用 Django 的组和权限。 For example, you can create custom permissions on your User's Meta class, like so:例如,您可以在用户的 Meta class 上创建自定义权限,如下所示:

class CustomUser(AbstractUser):
    # ...
    class Meta:
        permissions = (
            ("can_create_exam", "Can create exam"),
            ("can_view_user", "Can view user"),
        )

That will create those custom permissions.这将创建那些自定义权限。 You can assign those to users in Django's Admin, or programmatically, as further explained in that article or Django's documentation.您可以将这些分配给 Django Admin 中的用户,或者以编程方式,如该文章或 Django 文档中进一步说明的那样。

There are several ways of testing if a user has those permissions, ie: use the permission_required decorator, or when you have a user instance in your view: user.has_perm('appname.can_create_exam') , or from a template: {% if perms.appname.can_create_exam %} .有几种方法可以测试用户是否具有这些权限,即:使用permission_required装饰器,或者当您的视图中有用户实例时: user.has_perm('appname.can_create_exam') ,或者从模板: {% if perms.appname.can_create_exam %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM