| 1 |
# django-guardian |
| 2 |
|
| 3 |
`django-guardian` is an implementation of _per-object permissions_ on top |
| 4 |
of Django’s authorization backend. Read an introduction to per-object permissions [on djangoadvent articles](https://github.com/djangoadvent/djangoadvent-articles/blob/master/1.2/06_object-permissions.rst). |
| 5 |
|
| 6 |
## Documentation |
| 7 |
|
| 8 |
Online documentation is available at [https://django-guardian.readthedocs.io/](https://django-guardian.readthedocs.io/). |
| 9 |
|
| 10 |
**See real-world usage**: Check out [Who Uses Guardian](https://django-guardian.readthedocs.io/en/stable/who-uses-guardian/) to see how thousands of projects worldwide use django-guardian in production. |
| 11 |
|
| 12 |
## Contributing |
| 13 |
|
| 14 |
Please read [`CONTRIBUTING.md`](https://github.com/django-guardian/django-guardian/blob/main/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. |
| 15 |
|
| 16 |
If you need to report a security issue, do **not** open a public issue. Follow [`SECURITY.md`](https://github.com/django-guardian/django-guardian/blob/main/SECURITY.md) and use the repository's [private vulnerability reporting flow](https://github.com/django-guardian/django-guardian/security). |
| 17 |
|
| 18 |
|
| 19 |
## Installation |
| 20 |
|
| 21 |
To install `django-guardian` into your project run: |
| 22 |
|
| 23 |
```bash |
| 24 |
uv add django-guardian |
| 25 |
``` |
| 26 |
> **TIP**: Not using a package manager like `uv` or `poetry` for your django project? You probably should try them :). In the meantime, `pip install django-guardian` works just fine too. |
| 27 |
|
| 28 |
|
| 29 |
## Configuration |
| 30 |
|
| 31 |
We need to hook `django-guardian` into our project. |
| 32 |
|
| 33 |
1. Put `guardian` into your `INSTALLED_APPS` at settings module: |
| 34 |
|
| 35 |
```python |
| 36 |
INSTALLED_APPS = ( |
| 37 |
... |
| 38 |
'guardian', |
| 39 |
) |
| 40 |
``` |
| 41 |
|
| 42 |
2. Add extra authorization backend to your `settings.py`: |
| 43 |
|
| 44 |
```py |
| 45 |
AUTHENTICATION_BACKENDS = ( |
| 46 |
'django.contrib.auth.backends.ModelBackend', |
| 47 |
'guardian.backends.ObjectPermissionBackend', |
| 48 |
) |
| 49 |
``` |
| 50 |
|
| 51 |
3. Create `guardian` database tables by running: |
| 52 |
|
| 53 |
``` |
| 54 |
python manage.py migrate |
| 55 |
``` |
| 56 |
|
| 57 |
## Usage |
| 58 |
|
| 59 |
After installation and project hooks we can finally use object permissions |
| 60 |
with Django. |
| 61 |
|
| 62 |
Lets start really quickly: |
| 63 |
|
| 64 |
```py |
| 65 |
>>> from django.contrib.auth.models import User, Group |
| 66 |
>>> jack = User.objects.create_user('jack', '[email protected]', 'topsecretagentjack') |
| 67 |
>>> admins = Group.objects.create(name='admins') |
| 68 |
>>> jack.has_perm('change_group', admins) |
| 69 |
False |
| 70 |
>>> from guardian.shortcuts import assign_perm |
| 71 |
>>> assign_perm('change_group', jack, obj=admins) |
| 72 |
<UserObjectPermission: admins | jack | change_group> |
| 73 |
>>> jack.has_perm('change_group', admins) |
| 74 |
True |
| 75 |
``` |
| 76 |
|
| 77 |
Of course our agent jack here would not be able to _change_group_ globally: |
| 78 |
|
| 79 |
```py |
| 80 |
>>> jack.has_perm('change_group') |
| 81 |
False |
| 82 |
``` |
| 83 |
|
| 84 |
## Admin integration |
| 85 |
|
| 86 |
Replace `admin.ModelAdmin` with `GuardedModelAdmin` for those models |
| 87 |
which should have object permissions support within admin panel. |
| 88 |
|
| 89 |
For example: |
| 90 |
|
| 91 |
```py |
| 92 |
from django.contrib import admin |
| 93 |
from myapp.models import Author |
| 94 |
from guardian.admin import GuardedModelAdmin |
| 95 |
|
| 96 |
# Old way: |
| 97 |
#class AuthorAdmin(admin.ModelAdmin): |
| 98 |
# pass |
| 99 |
|
| 100 |
# With object permissions support |
| 101 |
class AuthorAdmin(GuardedModelAdmin): |
| 102 |
pass |
| 103 |
|
| 104 |
admin.site.register(Author, AuthorAdmin) |
| 105 |
``` |
| 106 |
|
| 107 |
## Django Unfold integration |
| 108 |
|
| 109 |
Users of [`django-unfold`](https://unfoldadmin.com/) will find that `guardian` is [supported out of the box via a `contrib` module](https://unfoldadmin.com/docs/integrations/django-guardian/). |