Customize group permissions in django?

I need to make role-based authentication. Firstly, I made group permissions for all my apps. I assigned some of them to role according to role's ability.

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

class Role(models.Model):
    title = models.CharField(max_length=32)
    groups = models.ManyToManyField(Group)

    def __str__(self) -> str:
        return self.title

driver role with access

Here you can a see driver role which has access on some group permissions (in fact they are just names of groups)

Now I have a user model:

class Users(AbstractUser):
    role = models.ForeignKey(
        Role, on_delete=models.CASCADE, related_name='roles', blank=True, null=True)

    def __str__(self):
        return (self.username)

I don't want to pick group permissions every time when I created a new staff user. I just want to set a role to a user and it should automatically pick all permissions based on selected role's groups. How to customize groups in django models?

Back to Top