0
# Signals
1
2
Django signal system integration for reacting to preference changes. The signals module provides Django signals that are emitted when preferences are updated, allowing you to hook into preference changes for custom logic, logging, or cache invalidation.
3
4
## Capabilities
5
6
### Preference Updated Signal
7
8
Signal emitted whenever a preference value is updated through any mechanism (manager, admin, API, forms). This signal is sent after the preference value has been successfully changed in the database.
9
10
```python { .api }
11
from django.dispatch import Signal
12
13
preference_updated: Signal
14
```
15
16
**Signal Arguments:**
17
- `sender`: The preference class that was updated
18
- `section`: Section name of the updated preference
19
- `name`: Name of the updated preference
20
- `old_value`: Previous value of the preference
21
- `new_value`: New value of the preference
22
- `instance`: The preference model instance that was updated
23
24
### Usage Examples
25
26
**Connecting to the Signal:**
27
28
```python
29
from dynamic_preferences.signals import preference_updated
30
from django.dispatch import receiver
31
32
@receiver(preference_updated)
33
def handle_preference_change(sender, section, name, old_value, new_value, instance, **kwargs):
34
"""Handle preference updates for logging or cache invalidation."""
35
print(f"Preference {section}__{name} changed from {old_value} to {new_value}")
36
37
# Example: Clear cache when maintenance mode changes
38
if section == 'general' and name == 'maintenance_mode':
39
from django.core.cache import cache
40
cache.clear()
41
42
# Alternative connection method
43
def notify_on_preference_update(sender, section, name, old_value, new_value, instance, **kwargs):
44
"""Send notification when important preferences change."""
45
if section in ['email', 'notifications']:
46
# Send notification to administrators
47
pass
48
49
preference_updated.connect(notify_on_preference_update)
50
```
51
52
**Using in Django Apps:**
53
54
```python
55
# apps.py
56
from django.apps import AppConfig
57
from dynamic_preferences.signals import preference_updated
58
59
class MyAppConfig(AppConfig):
60
default_auto_field = 'django.db.models.BigAutoField'
61
name = 'myapp'
62
63
def ready(self):
64
preference_updated.connect(self.handle_preference_update)
65
66
def handle_preference_update(self, sender, section, name, old_value, new_value, instance, **kwargs):
67
# Custom handling logic
68
pass
69
```
70
71
**Filtering by Preference Type:**
72
73
```python
74
@receiver(preference_updated)
75
def handle_site_preferences(sender, section, name, old_value, new_value, instance, **kwargs):
76
"""Only handle site-wide configuration changes."""
77
if section == 'site_config':
78
# Rebuild static files, clear CDN cache, etc.
79
pass
80
```
81
82
The signal is automatically emitted when preferences are updated through:
83
- PreferencesManager (programmatic updates)
84
- Django Admin interface
85
- REST API endpoints
86
- Form submissions