Dynamic global and instance settings for your django project
—
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.
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.
from django.dispatch import Signal
preference_updated: SignalSignal Arguments:
sender: The preference class that was updatedsection: Section name of the updated preferencename: Name of the updated preferenceold_value: Previous value of the preferencenew_value: New value of the preferenceinstance: The preference model instance that was updatedConnecting to the Signal:
from dynamic_preferences.signals import preference_updated
from django.dispatch import receiver
@receiver(preference_updated)
def handle_preference_change(sender, section, name, old_value, new_value, instance, **kwargs):
"""Handle preference updates for logging or cache invalidation."""
print(f"Preference {section}__{name} changed from {old_value} to {new_value}")
# Example: Clear cache when maintenance mode changes
if section == 'general' and name == 'maintenance_mode':
from django.core.cache import cache
cache.clear()
# Alternative connection method
def notify_on_preference_update(sender, section, name, old_value, new_value, instance, **kwargs):
"""Send notification when important preferences change."""
if section in ['email', 'notifications']:
# Send notification to administrators
pass
preference_updated.connect(notify_on_preference_update)Using in Django Apps:
# apps.py
from django.apps import AppConfig
from dynamic_preferences.signals import preference_updated
class MyAppConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'myapp'
def ready(self):
preference_updated.connect(self.handle_preference_update)
def handle_preference_update(self, sender, section, name, old_value, new_value, instance, **kwargs):
# Custom handling logic
passFiltering by Preference Type:
@receiver(preference_updated)
def handle_site_preferences(sender, section, name, old_value, new_value, instance, **kwargs):
"""Only handle site-wide configuration changes."""
if section == 'site_config':
# Rebuild static files, clear CDN cache, etc.
passThe signal is automatically emitted when preferences are updated through:
Install with Tessl CLI
npx tessl i tessl/pypi-django-dynamic-preferences