Debian Codegraph @

README.md

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 # django-guardian
2
3 [![Tests](https://github.com/django-guardian/django-guardian/actions/workflows/tests.yml/badge.svg)](https://github.com/django-guardian/django-guardian/actions/workflows/tests.yml)
4 [![Coverage](https://img.shields.io/badge/coverage-97.8%25-brightgreen)](https://github.com/django-guardian/django-guardian/actions/workflows/coverage.yml)
5 [![PyPI version](https://img.shields.io/pypi/v/django-guardian.svg)](https://pypi.python.org/pypi/django-guardian)
6 [![Python versions](https://img.shields.io/pypi/pyversions/django-guardian.svg)](https://pypi.python.org/pypi/django-guardian)
7 [![Published on Django Packages](https://img.shields.io/badge/Published%20on-Django%20Packages-0c3c26)](https://djangopackages.org/packages/p/django-guardian/)
8
9 `django-guardian` is an implementation of _per-object permissions_ on top
10 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).
11
12 ## Documentation
13
14 Online documentation is available at [https://django-guardian.readthedocs.io/](https://django-guardian.readthedocs.io/).
15
16 **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.
17
18 ## Contributing
19
20 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.
21
22 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).
23
24
25 ## Installation
26
27 To install `django-guardian` into your project run:
28
29 ```bash
30 uv add django-guardian
31 ```
32 > **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.
33
34
35 ## Configuration
36
37 We need to hook `django-guardian` into our project.
38
39 1. Put `guardian` into your `INSTALLED_APPS` at settings module:
40
41 ```python
42 INSTALLED_APPS = (
43 ...
44 'guardian',
45 )
46 ```
47
48 2. Add extra authorization backend to your `settings.py`:
49
50 ```py
51 AUTHENTICATION_BACKENDS = (
52 'django.contrib.auth.backends.ModelBackend',
53 'guardian.backends.ObjectPermissionBackend',
54 )
55 ```
56
57 3. Create `guardian` database tables by running:
58
59 ```
60 python manage.py migrate
61 ```
62
63 ## Usage
64
65 After installation and project hooks we can finally use object permissions
66 with Django.
67
68 Lets start really quickly:
69
70 ```py
71 >>> from django.contrib.auth.models import User, Group
72 >>> jack = User.objects.create_user('jack', '[email protected]', 'topsecretagentjack')
73 >>> admins = Group.objects.create(name='admins')
74 >>> jack.has_perm('change_group', admins)
75 False
76 >>> from guardian.shortcuts import assign_perm
77 >>> assign_perm('change_group', jack, obj=admins)
78 <UserObjectPermission: admins | jack | change_group>
79 >>> jack.has_perm('change_group', admins)
80 True
81 ```
82
83 Of course our agent jack here would not be able to _change_group_ globally:
84
85 ```py
86 >>> jack.has_perm('change_group')
87 False
88 ```
89
90 ## Admin integration
91
92 Replace `admin.ModelAdmin` with `GuardedModelAdmin` for those models
93 which should have object permissions support within admin panel.
94
95 For example:
96
97 ```py
98 from django.contrib import admin
99 from myapp.models import Author
100 from guardian.admin import GuardedModelAdmin
101
102 # Old way:
103 #class AuthorAdmin(admin.ModelAdmin):
104 # pass
105
106 # With object permissions support
107 class AuthorAdmin(GuardedModelAdmin):
108 pass
109
110 admin.site.register(Author, AuthorAdmin)
111 ```
112
113 ## Django Unfold integration
114
115 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/).