| 1 |
--- |
| 2 |
title: Configuration |
| 3 |
description: Configuration options and django settings for django-guardian. |
| 4 |
--- |
| 5 |
|
| 6 |
# Configuration |
| 7 |
|
| 8 |
After [installation](./installation.md), we can prepare our project for object permissions handling. |
| 9 |
In a settings module we need to add guardian to `INSTALLED_APPS`: |
| 10 |
|
| 11 |
```python |
| 12 |
INSTALLED_APPS = ( |
| 13 |
# ... |
| 14 |
'guardian', |
| 15 |
) |
| 16 |
``` |
| 17 |
|
| 18 |
and hook guardian's authentication backend: |
| 19 |
|
| 20 |
```python |
| 21 |
AUTHENTICATION_BACKENDS = ( |
| 22 |
'django.contrib.auth.backends.ModelBackend', # this is default |
| 23 |
'guardian.backends.ObjectPermissionBackend', |
| 24 |
) |
| 25 |
``` |
| 26 |
|
| 27 |
!!! note |
| 28 |
|
| 29 |
Once project is configured to work with `django-guardian`, calling |
| 30 |
`migrate` management command would create `User` instance for anonymous |
| 31 |
user support (with name of `AnonymousUser`). |
| 32 |
|
| 33 |
!!! note |
| 34 |
|
| 35 |
The Guardian anonymous user is different from the Django Anonymous user. |
| 36 |
The Django Anonymous user does not have an entry in the database, |
| 37 |
however the Guardian anonymous user does. This means that the following |
| 38 |
code will return an unexpected result: |
| 39 |
|
| 40 |
``` python |
| 41 |
from django.contrib.auth import get_user_model |
| 42 |
User = get_user_model() |
| 43 |
anon = User.get_anonymous() |
| 44 |
anon.is_anonymous # returns False |
| 45 |
``` |
| 46 |
|
| 47 |
|
| 48 |
We can change id to whatever we like. Project should be now ready to use |
| 49 |
object permissions. |
| 50 |
|
| 51 |
# Optional settings |
| 52 |
|
| 53 |
Guardian has the following, optional configuration variables: |
| 54 |
|
| 55 |
## `GUARDIAN_RAISE_403` |
| 56 |
|
| 57 |
!!! abstract "Added in version 1.0.4" |
| 58 |
|
| 59 |
If set to `True`, guardian would raise `django.core.exceptions.PermissionDenied` error instead of returning |
| 60 |
empty `django.http.HttpResponseForbidden`. |
| 61 |
|
| 62 |
!!! warning |
| 63 |
|
| 64 |
Remember that you cannot use both |
| 65 |
`GUARDIAN_RENDER_403` **AND** `GUARDIAN_RAISE_403` - if both are set |
| 66 |
to `True`, `django.core.exceptions.ImproperlyConfigured` would be raised. |
| 67 |
|
| 68 |
## `GUARDIAN_RENDER_403` |
| 69 |
|
| 70 |
!!! abstract "Added in version 1.0.4" |
| 71 |
|
| 72 |
If set to `True`, guardian would try to render 403 response rather than |
| 73 |
return content less `django.http.HttpResponseForbidden`. |
| 74 |
Would use template pointed by `GUARDIAN_TEMPLATE_403` to do that. Default is `False`. |
| 75 |
|
| 76 |
!!! warning |
| 77 |
|
| 78 |
Remember that you cannot use both |
| 79 |
`GUARDIAN_RENDER_403`{.interpreted-text role="setting"} **AND** |
| 80 |
`GUARDIAN_RAISE_403`{.interpreted-text role="setting"} - if both are set |
| 81 |
to `True`, `django.core.exceptions.ImproperlyConfigured` would be |
| 82 |
raised. |
| 83 |
|
| 84 |
## `GUARDIAN_TEMPLATE_403` |
| 85 |
|
| 86 |
!!! abstract "Added in version 1.0.4" |
| 87 |
|
| 88 |
Tells parts of guardian what template to use for responses with status |
| 89 |
code `403` (i.e. `api-decorators-permission_required`). Defaults to `403.html`. |
| 90 |
|
| 91 |
|
| 92 |
## `ANONYMOUS_USER_NAME` |
| 93 |
|
| 94 |
!!! abstract "Added in version 1.4.2" |
| 95 |
|
| 96 |
This is the username of the anonymous user. Used to create the anonymous |
| 97 |
user and subsequently fetch the anonymous user as required. |
| 98 |
|
| 99 |
If `ANONYMOUS_USER_NAME` is set to `None`, anonymous user object |
| 100 |
permissions-are disabled. You may need to choose this option if creating |
| 101 |
an `User` object-to represent anonymous users would be problematic in |
| 102 |
your environment. |
| 103 |
|
| 104 |
Defaults to `"AnonymousUser"`. |
| 105 |
|
| 106 |
|
| 107 |
!!! tip |
| 108 |
[Django's docs on substituting a custom user model](https://docs.djangoproject.com/en/stable/topics/auth/customizing/#substituting-a-custom-user-model) |
| 109 |
|
| 110 |
## `GUARDIAN_GET_INIT_ANONYMOUS_USER` |
| 111 |
|
| 112 |
!!! abstract "Added in version 1.2" |
| 113 |
|
| 114 |
Guardian supports object level permissions for anonymous users, however, |
| 115 |
when in our project we use a custom User model, default function might |
| 116 |
fail. This can lead to issues as `guardian` tries to create anonymous |
| 117 |
user after each `migrate` call. Object that is going to be created is |
| 118 |
retrieved using function pointed by this setting. Once retrieved, `save` |
| 119 |
method would be called on that instance. |
| 120 |
|
| 121 |
Defaults to `"guardian.management.get_init_anonymous_user"`. |
| 122 |
|
| 123 |
!!! tip "See also `custom-user-model-anonymous`" |
| 124 |
|
| 125 |
## `GUARDIAN_GET_CONTENT_TYPE` |
| 126 |
|
| 127 |
!!! info "Added in 1.5" |
| 128 |
|
| 129 |
Guardian allows applications to supply a custom function to retrieve the |
| 130 |
content type from objects and models. This is useful when a class or |
| 131 |
class hierarchy uses the `ContentType` framework in an non-standard way. |
| 132 |
Most applications will not have to change this setting. |
| 133 |
|
| 134 |
As an example, when using `django-polymorphic` it's useful to use a |
| 135 |
permission on the base model which applies to all child models. In this |
| 136 |
case, the custom function would return the `ContentType` of the base |
| 137 |
class for polymorphic models and the regular model `ContentType` for |
| 138 |
non-polymorphic classes. |
| 139 |
|
| 140 |
Defaults to `"guardian.ctypes.get_default_content_type"`. |
| 141 |
|
| 142 |
## `GUARDIAN_AUTO_PREFETCH` |
| 143 |
|
| 144 |
!!! abstract "Added in version 2.0.0" |
| 145 |
|
| 146 |
For vanilla deployments using standard `ContentType` interfaces and |
| 147 |
default `UserObjectPermission` or `GroupObjectPermission` models, |
| 148 |
Guardian can automatically prefetch all User permissions for all object |
| 149 |
types. This can be useful when manual prefetching is not feasible due to |
| 150 |
a large number of model types resulting in O(n) queries. This setting |
| 151 |
may not be compatible with non-standard deployments, and should only be |
| 152 |
used when non-prefetched invocations would result in a large number of |
| 153 |
queries or when latency is particularly important. |
| 154 |
|
| 155 |
Defaults to `False`. |
| 156 |
|
| 157 |
## `GUARDIAN_ANONYMOUS_USER_CACHE_TTL` |
| 158 |
|
| 159 |
!!! abstract "Added in version 3.1.3" |
| 160 |
|
| 161 |
Sets the cache timeout (in seconds) for the anonymous user. This setting controls |
| 162 |
both whether caching is enabled and how long the anonymous user instance will |
| 163 |
remain in the cache before being refreshed from the database. |
| 164 |
|
| 165 |
This setting uses Django's cache framework, making it compatible with |
| 166 |
all Django cache backends (Redis, Memcached, database cache, etc.) |
| 167 |
and supporting distributed deployments. |
| 168 |
|
| 169 |
```python |
| 170 |
# No cache (default) |
| 171 |
GUARDIAN_ANONYMOUS_USER_CACHE_TTL = 0 |
| 172 |
|
| 173 |
# Cache anonymous user for 1 hour |
| 174 |
GUARDIAN_ANONYMOUS_USER_CACHE_TTL = 3600 |
| 175 |
|
| 176 |
# Cache anonymous user indefinitely (not recommended) |
| 177 |
GUARDIAN_ANONYMOUS_USER_CACHE_TTL = -1 |
| 178 |
``` |
| 179 |
|
| 180 |
!!! tip "Performance tuning" |
| 181 |
|
| 182 |
- For high-traffic applications: Use shorter TTL (300-600 seconds) |
| 183 |
- For stable applications: Use longer TTL (1800-3600 seconds) |
| 184 |
- Consider your cache backend's memory limits when setting TTL |
| 185 |
|
| 186 |
!!! warning "Cache persistence" |
| 187 |
|
| 188 |
When caching is enabled (TTL > 0 or TTL = -1), the cache persists for the |
| 189 |
specified duration or until the cache backend evicts it. If you manually |
| 190 |
change the anonymous user in the database during runtime (which is not |
| 191 |
recommended), cached entries will not reflect the changes until they expire. |
| 192 |
|
| 193 |
!!! note "Cache backend dependency" |
| 194 |
|
| 195 |
The actual caching behavior depends on your Django cache |
| 196 |
configuration (`CACHES` setting). When TTL is 0, no caching occurs |
| 197 |
and each call to `get_anonymous_user()` performs a fresh database query. |
| 198 |
|
| 199 |
## `GUARDIAN_USER_OBJ_PERMS_MODEL` |
| 200 |
|
| 201 |
!!! abstract "Added in version 2.0.0" |
| 202 |
|
| 203 |
Allows the default `UserObjectPermission` model to be overridden by a |
| 204 |
custom model. The custom model needs to minimally inherit from |
| 205 |
`UserObjectPermissionAbstract`. This is only automatically supported |
| 206 |
when set at the start of a project. This is NOT supported after the |
| 207 |
start of a project. If the dependent libraries do not call |
| 208 |
`UserObjectPermission = get_user_obj_perms_model()` for the model, then |
| 209 |
the dependent library does not support this feature. |
| 210 |
|
| 211 |
Define a custom user object permission model |
| 212 |
|
| 213 |
```python |
| 214 |
from guardian.models import UserObjectPermissionAbstract |
| 215 |
|
| 216 |
class BigUserObjectPermission(UserObjectPermissionAbstract): |
| 217 |
id = models.BigAutoField(editable=False, unique=True, primary_key=True) |
| 218 |
|
| 219 |
class Meta(UserObjectPermissionAbstract.Meta): |
| 220 |
abstract = False |
| 221 |
indexes = [ |
| 222 |
*UserObjectPermissionAbstract.Meta.indexes, |
| 223 |
models.Index(fields=['content_type', 'object_pk', 'user']), |
| 224 |
] |
| 225 |
``` |
| 226 |
|
| 227 |
Configure guardian to use the custom model in `settings.py` :: |
| 228 |
|
| 229 |
```python |
| 230 |
GUARDIAN_USER_OBJ_PERMS_MODEL = "myapp.BigUserObjectPermission" |
| 231 |
``` |
| 232 |
|
| 233 |
|
| 234 |
To access the model use `get_user_obj_perms_model()` with no parameters |
| 235 |
|
| 236 |
```python |
| 237 |
from guardian.utils import get_user_obj_perms_model |
| 238 |
UserObjectPermission = get_user_obj_perms_model() |
| 239 |
``` |
| 240 |
|
| 241 |
Defaults to `'guardian.UserObjectPermission'`. |
| 242 |
|
| 243 |
## `GUARDIAN_GROUP_OBJ_PERMS_MODEL` |
| 244 |
|
| 245 |
!!! abstract "Added in version 2.0.0" |
| 246 |
|
| 247 |
Allows the default `GroupObjectPermission` model to be overridden by a |
| 248 |
custom model. The custom model needs to minimally inherit from |
| 249 |
`GroupObjectPermissionAbstract`. This is only automatically supported |
| 250 |
when set at the start of a project. This is NOT supported after the |
| 251 |
start of a project. If the dependent libraries do not call |
| 252 |
`GroupObjectPermission = get_user_obj_perms_model()` for the model, then |
| 253 |
the dependent library does not support this feature. |
| 254 |
|
| 255 |
Define a custom user object permission model: |
| 256 |
|
| 257 |
```python |
| 258 |
from guardian.models import GroupObjectPermissionAbstract |
| 259 |
|
| 260 |
class BigGroupObjectPermission(GroupObjectPermissionAbstract): |
| 261 |
id = models.BigAutoField(editable=False, unique=True, primary_key=True) |
| 262 |
|
| 263 |
class Meta(GroupObjectPermissionAbstract.Meta): |
| 264 |
abstract = False |
| 265 |
indexes = [ |
| 266 |
*GroupObjectPermissionAbstract.Meta.indexes, |
| 267 |
models.Index(fields=['content_type', 'object_pk', 'group']), |
| 268 |
] |
| 269 |
``` |
| 270 |
|
| 271 |
Configure guardian to use the custom model in settings.py |
| 272 |
```python |
| 273 |
GUARDIAN_GROUP_OBJ_PERMS_MODEL = 'myapp.BigGroupObjectPermission' |
| 274 |
``` |
| 275 |
|
| 276 |
To access the model use `get_group_obj_perms_model()` with no parameters |
| 277 |
|
| 278 |
```python |
| 279 |
from guardian.utils import get_group_obj_perms_model |
| 280 |
GroupObjectPermission = get_group_obj_perms_model() |
| 281 |
``` |
| 282 |
|
| 283 |
Defaults to `'guardian.GroupObjectPermission'`. |