0

I have created a role model for Employee so that employee will be assigned to control the overall app based on his/her role. I mean if the role of employee is given can_create_only, then the employee should be able to create inventory, orders, items etc and if employee is given can_create_edit_and_delete, then the employee would be like one of the admin and etc. For this I have designed the model as below but I want to know what is the best way to handle such and why?

Should I go with middleware or decorator way? Can anyone give me an example, please?

class Role(models.Model):
    name = models.CharField(max_length=100, blank=False, null=False)

    class Meta:
        verbose_name = 'Role'
        verbose_name_plural = 'Roles'


class Employee(models.Model):
    office = models.ForeignKey(
        OfficeSetup, blank=False, null=False, on_delete=models.CASCADE)
    name = models.CharField(max_length=150, blank=False, null=False)
    designation = models.ForeignKey(Designation, blank=False, null=False)
    section = models.ForeignKey(DepartmentSetup, blank=True, null=True)
    phone_number = models.CharField(max_length=150, blank=True, null=True)
    mobile_number = models.CharField(max_length=150, blank=True, null=True)
    email = models.EmailField(max_length=150, blank=False, null=False)
    gender = models.CharField(
        max_length=4, choices=GENDER, blank=True, null=True)
    role = models.ForeignKey(Role, blank=True, null=True)
    username = models.CharField(max_length=100, blank=False, null=False)
    password = models.CharField(max_length=100, blank=False, null=False)
    avatar = models.ImageField(
        null=True, blank=True, upload_to=upload_employee_image_path)

    class Meta:
        verbose_name = 'Employee'
        verbose_name_plural = 'Employees'

    def __str__(self):
        return self.name

When creating an employee by admin, the username, password and email, the admin provides will create a new user instance along with the employee

Serenity
  • 3,884
  • 6
  • 44
  • 87

1 Answers1

1

Django comes with Groups and permissions which provides all most everything you are looking for. This may help you - How do I use Django groups and permissions? Django documentation - https://docs.djangoproject.com/en/1.11/topics/auth/

  • Can you show me an example, please? Documentation I felt difficult to understand – Serenity Nov 23 '17 at 07:11
  • If I am not wrong, the documentation shows setting permission for specific models only but my use case is setting permission for all the models at once – Serenity Nov 23 '17 at 09:03