[Django]-Django groups, roles and permissions

2👍

If you do not need any specific privileges for each employee title, then choices would be pretty simple to implement like below

Sample Example

from django.db import models

class Employee(models.Model):
    SALES_MANAGER = 1
    HR_MANAGER = 2
    ENGINEERING_MANAGER = 3
  
    ROLE_CHOICES = (
      (SALES_MANAGER, 'Sales Manager'),
      (HR_MANAGER, 'HR Manager'),
      (ENGINEERING_MANAGER, 'Manager'),
    )
    employee_title = models.CharField(max_length=100, choices=ROLE_CHOICES, default='Manager')

But do note that if you want to add new employee title’s then a re-run of migrations would be required. If you need to avoid this then groups would be a better choice.

from django.db import models
from django.contrib.auth.models import Group

class Employee(models.Model):
    employee_title = models.ManyToManyField(Group)

With groups, you would be able to create new entries without any migrations directly from admin panel.

3👍

The django groups, role and permissions system is for allow or denay action in administration pannel, for this reason these three components work together.

  1. If in your application all these type of user have access in admin pannel I suggestion you to use the Groups, roles and permission system
  2. But If your users haven’t the access to admin pannel you can avoid using it.

In first option you can create a different roles for every users and allow some permissions for each but if you have groups of users with same permission you can regroup they in a group. For more info view this https://docs.djangoproject.com/en/4.0/topics/auth/default/#permissions-and-authorization

Leave a comment