| 1 |
--- |
| 2 |
title: Signals |
| 3 |
description: How to react to object permission changes using Django signals. |
| 4 |
--- |
| 5 |
|
| 6 |
# Reacting to Permission Changes with Signals |
| 7 |
|
| 8 |
Django Guardian does not currently ship dedicated signals for permission changes. |
| 9 |
However, you can use Django's built-in `post_save` and `post_delete` signals on |
| 10 |
Guardian's permission models to react when object permissions are created or |
| 11 |
removed. |
| 12 |
|
| 13 |
## Connecting to Permission Model Signals |
| 14 |
|
| 15 |
Guardian stores object permissions in `UserObjectPermission` and |
| 16 |
`GroupObjectPermission` models (or your custom replacements). Because these are |
| 17 |
regular Django models, you can connect to their `post_save` and `post_delete` |
| 18 |
signals just like any other model. |
| 19 |
|
| 20 |
Wire the signals inside your `AppConfig.ready()` method: |
| 21 |
|
| 22 |
``` python |
| 23 |
from django.apps import AppConfig |
| 24 |
from django.db.models.signals import post_save, post_delete |
| 25 |
|
| 26 |
|
| 27 |
class MyAppConfig(AppConfig): |
| 28 |
name = "myapp" |
| 29 |
|
| 30 |
def ready(self): |
| 31 |
from guardian.utils import ( |
| 32 |
get_user_obj_perms_model, |
| 33 |
get_group_obj_perms_model, |
| 34 |
) |
| 35 |
|
| 36 |
UserObjectPermission = get_user_obj_perms_model() |
| 37 |
GroupObjectPermission = get_group_obj_perms_model() |
| 38 |
post_save.connect( |
| 39 |
self.handle_perm_change, |
| 40 |
sender=UserObjectPermission, |
| 41 |
dispatch_uid="myapp.handle_perm_change.userobjectpermission.post_save", |
| 42 |
) |
| 43 |
post_save.connect( |
| 44 |
self.handle_perm_change, |
| 45 |
sender=GroupObjectPermission, |
| 46 |
dispatch_uid="myapp.handle_perm_change.groupobjectpermission.post_save", |
| 47 |
) |
| 48 |
post_delete.connect( |
| 49 |
self.handle_perm_change, |
| 50 |
sender=UserObjectPermission, |
| 51 |
dispatch_uid="myapp.handle_perm_change.userobjectpermission.post_delete", |
| 52 |
) |
| 53 |
post_delete.connect( |
| 54 |
self.handle_perm_change, |
| 55 |
sender=GroupObjectPermission, |
| 56 |
dispatch_uid="myapp.handle_perm_change.groupobjectpermission.post_delete", |
| 57 |
) |
| 58 |
|
| 59 |
@staticmethod |
| 60 |
def handle_perm_change(sender, instance, **kwargs): |
| 61 |
# React to the permission change here. |
| 62 |
# For example: invalidate a cache, write an audit log, or send a |
| 63 |
# notification. |
| 64 |
print(f"Permission changed: {instance}") |
| 65 |
``` |
| 66 |
|
| 67 |
!!! warning |
| 68 |
|
| 69 |
**Bulk operations do not fire `post_save` signals.** Methods like |
| 70 |
`bulk_assign_perm()` and `assign_perm_to_many()` use `bulk_create()` |
| 71 |
internally, so per-object `post_save` signals are not sent. If you rely on |
| 72 |
signals, assign permissions individually instead. See the |
| 73 |
[Assign Object Permissions](assign.md#limitations) page for details. |
| 74 |
|
| 75 |
## Further Reading |
| 76 |
|
| 77 |
This pattern was originally discussed in |
| 78 |
[GitHub issue #805](https://github.com/django-guardian/django-guardian/issues/805). |