Skip to main content

Add user roles to django-auth

Project description

Code Climate

Add roles to django-auth

Role are set of group of permissions and permissions. It’s fully customizable. Everything is in database.

Admin application allow to manage roles.


pip install django-auth-role

Quick start

Add authrole to INSTALLED_APPS (django.contrib.auth and django.contrib.contenttypes are also required) and AuthRoleBackend to AUTHENTICATION_BACKENDS.



Extend auth.User.

from authrole.mixins import RoleMixin
from django.db import models

class MyUser(RoleMixin, models.Model):
    user = models.OneToOneField('auth.User', related_name='user')

or create new auth user model:

from authrole.mixins import RoleMixin
from django.contrib.auth.models import AbstractUser
from django.db import models

class MyUser(RoleMixin, AbstractUser):

In this case remember to set AUTH_USER_MODEL to Your model.

Create tables.

./ migrate

Advanced usage

Own authentication backend

If You need Your own authentication backend, simply extend BaseAuthRoleBackend. fetch_role_permissions function must return a list of auth.Permission objects:

from authrole.auth.backends import BaseAuthRoleBackend
from django.contrib.auth.models import Permission

class MyBackend(BaseAuthRoleBackend):
    def fetch_role_permissions(self, user_obj):
        if user_obj.username == 'admin':
            return Permission.objects.all()
            return Permission.objects.none()

Extend role

Add OneToOneField to Your model:

from django.db import models

class MyRole(models.Model):
    role = models.OneToOneField('authrole.Role', null=False, blank=False, related_name='myrole')
    extra_field = models.CharField(max_length=10)

And use:

from authrole.models import Role

role = Role.objects.all()[0]


Or write Your own role class based on AbstractRole (Django >= 1.5):

from authrole.model import AbstractRole

class MyRole(AbstractRole):
    extra_field = models.CharField(max_length=10)

Point AUTHROLE_ROLE_MODEL to Your new model:


And use:

from app.models import MyRole

role = MyRole.objects.all()[0]


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for django-auth-role, version 0.4.0
Filename, size File type Python version Upload date Hashes
Filename, size django_auth_role-0.4.0-py2.py3-none-any.whl (12.5 kB) File type Wheel Python version 3.6 Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page