0
votes
  1. models.py =============== from django.contrib.auth.models import AbstractUser from django.db import models

       # Create your models here. class User(AbstractUser):    username = models.CharField(max_length=10, unique=True)   
    

    is_superuser = models.BooleanField(default=False) is_staff = models.BooleanField(default=False) is_admin = models.BooleanField(default=False) is_teacher = models.BooleanField(default=False) is_student = models.BooleanField(default=False)

       class Admin(models.Model):    user = models.OneToOneField(User, on_delete=models.CASCADE,
    

    related_name="admin_account")

       def __str__(self):
       return self.user.username
    
       class Teacher(models.Model):    user = models.OneToOneField(User, on_delete=models.CASCADE,
    

    related_name="teacher_account") def str(self): return self.user.username

       class Student(models.Model):    user = models.OneToOneField(User, on_delete=models.CASCADE,
    

    related_name="student_account") def str(self): return self.user.username

1
Check this out DRF permissionsRvector

1 Answers

0
votes

I've done something very similar before having students and teachers,but for me what I did was make the student and teacher model inherit from User class. With that in mind,my permission classes looked like this:

class IsStudent(permissions.BasePermission):
    def has_permission(self, request, view):
        return request.user.student and request.user.is_authenticated