Home > Net >  how to have multiple user types in django
how to have multiple user types in django

Time:05-06

this app is a school management system with django

i have three types of user in my web app, my problem is how to authorize these and give them permissions.

users are: teacher, student, school staff(admins)

i need to have different pages(apps) for each of them, like:

teacher -> login -> teacher_app.

student -> login -> student_app.

im new in django

my models are something like this:

class Student(models.Model) :
    national_id = models.CharField(
        max_length=50,
        unique=True,
    )

    name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    phone_number = models.CharField(max_length=50)

class Teacher(models.Model) :
    personal_id = models.CharField(
        max_length=50,
        unique=True,
    )

    name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)

    phone_number = models.CharField(
        max_length=50, 
        unique=True,
    )
    email = models.EmailField(
        max_length=250, 
        unique=True,
    )

class Class(models.Model):
    name = models.CharField(max_length=50)

    start_time = models.TimeField(
        auto_now=False, 
        auto_now_add=False,
        default=DEFAULT_START_TIME,
    )
    end_time = models.TimeField(
        auto_now=False,
        auto_now_add=False,
        default=DEFAULT_END_TIME,
    )

    # Relations
    teacher = models.ForeignKey(
        Teacher, 
        on_delete=models.CASCADE
    )
    students = models.ManyToManyField(Student)

admins will create classes and add students and teachers.

CodePudding user response:

Django has Roles and Groups to categorise users. What is allowed and forbidden is controlled by Permissions. Every User, Role and Group can have different model-specific and view-specific Permissions. The system is quite complex and allows for many different setups. I'd suggest you chek an overview such as this one.

CodePudding user response:

It is better to handle it with a field (not with 2 different models) for example you can have something like this:

class User(models.Model) :
    # NEW
    ROLES = (('student', 'Student'), ('teacher', 'Teacher'))
    role = models.CharField(
        max_length=20,
        choices=ROLES,
        default='student'
    )


    national_id = models.CharField(
        max_length=50,
        unique=True,
    )

    name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    phone_number = models.CharField(max_length=50)

Also, you can use the Django permission model more detail

  • Related