Django models related data

I have three models User, Employee, and Role. When a user logs in I want to user his role to redirect them to their corresponding pages.

Please these are models

class User(AbstractBaseUser):

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

email = models.CharField(unique=True, max_length=150)

object = UserManager()

USERNAME_FIELD = 'employee_id'

REQUIRED_FIELDS = []

def __str__(self):

    return self.employee_id

def has_perm(self, perm, obj=None):

    return True

def has_module_perms(self, app_label):

    return True

def is_staff(self):

    return self.email

class Employee(models.Model):

GENDER_CHOICE = [

    ('m', 'Male'),

    ('f', 'Female')

]

user = models.ForeignKey(User, on_delete=models.CASCADE)

first_name = models.CharField(max_length=100)

last_name = models.CharField(max_length=100)

phoneNumber = PhoneNumberField()

digital_address = models.CharField(max_length=15)

gender = models.CharField(choices = GENDER_CHOICE, max_length=1)

photo = models.ImageField(upload_to=employee_location)

role = models.ManyToManyField(Role)


def __str__(self):

    return self.first_name + ' ' + self.last_name

class Role(models.Model):

SUPERADMIN = 1

ADMIN = 2

EMPLOYEE = 3

ROLE_CHOICES = (

  (SUPERADMIN, 'super'),

  (ADMIN, 'admin'),

  (EMPLOYEE, 'employee'),

)

id = models.PositiveSmallIntegerField(choices=ROLE_CHOICES, primary_key=True)

def str(self):

  return self.get_id_display()

You’ve got a ManyToMany relationship between Employee and Role. There’s also a ManyToOne relationship between Employee and User.

This means that each User many be related to multiple Employees, and each Employee may have multiple roles.

What’s the business logic for determining exactly which page the User should be directed to upon login?

(I have a bit of a suspicion that your models aren’t set up to do what I think you may want them to do.)

Ken

1 Like