/
README (full)
django-guardian
django-guardian is an implementation of per-object permissions on top
of Django’s authorization backend. Read an introduction to per-object permissions on djangoadvent articles.
Documentation
Online documentation is available at https://django-guardian.readthedocs.io/.
See real-world usage: Check out Who Uses Guardian to see how thousands of projects worldwide use django-guardian in production.
Contributing
Please read CONTRIBUTING.md before opening a pull request. It documents the repository's issue-first workflow, branch targeting rules, local uv setup, required test and lint commands, documentation expectations, and AI/LLM contribution policy.
If you need to report a security issue, do not open a public issue. Follow SECURITY.md and use the repository's private vulnerability reporting flow.
Installation
To install django-guardian into your project run:
uv add django-guardian
TIP: Not using a package manager like
uvorpoetryfor your django project? You probably should try them :). In the meantime,pip install django-guardianworks just fine too.
Configuration
We need to hook django-guardian into our project.
- Put
guardianinto yourINSTALLED_APPSat settings module:
INSTALLED_APPS = (
...
'guardian',
)
- Add extra authorization backend to your
settings.py:
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
'guardian.backends.ObjectPermissionBackend',
)
- Create
guardiandatabase tables by running:
python manage.py migrate
Usage
After installation and project hooks we can finally use object permissions with Django.
Lets start really quickly:
>>> from django.contrib.auth.models import User, Group
>>> jack = User.objects.create_user('jack', '[email protected]', 'topsecretagentjack')
>>> admins = Group.objects.create(name='admins')
>>> jack.has_perm('change_group', admins)
False
>>> from guardian.shortcuts import assign_perm
>>> assign_perm('change_group', jack, obj=admins)
<UserObjectPermission: admins | jack | change_group>
>>> jack.has_perm('change_group', admins)
True
Of course our agent jack here would not be able to change_group globally:
>>> jack.has_perm('change_group')
False
Admin integration
Replace admin.ModelAdmin with GuardedModelAdmin for those models
which should have object permissions support within admin panel.
For example:
from django.contrib import admin
from myapp.models import Author
from guardian.admin import GuardedModelAdmin
# Old way:
#class AuthorAdmin(admin.ModelAdmin):
# pass
# With object permissions support
class AuthorAdmin(GuardedModelAdmin):
pass
admin.site.register(Author, AuthorAdmin)
Django Unfold integration
Users of django-unfold will find that guardian is supported out of the box via a contrib module.
| dir | .github/ |
| dir | benchmarks/ |
| dir | debian/ |
| dir | docs/ |
| dir | example_project/ |
| dir | example_project_custom_group/ |
| dir | guardian/ |
| file | .gitignore |
| file | .pre-commit-config.yaml |
| file | .readthedocs.yaml |
| file | CODE_OF_CONDUCT.md |
| file | CONTRIBUTING.md |
| file | LICENSE |
| file | MANIFEST.in |
| file | Makefile |
| file | README.md |
| file | SECURITY.md |
| file | manage.py |
| file | mkdocs.yml |
| file | pyproject.toml |
| file | tox.ini |