I am confused about the use of signals. Where should I put the signal codes so that they are orderly and appropriate? I have a signal file for an app. Where do I call it?
I am confused about the use of signals. Where should I put the signal codes so that they are orderly and appropriate?
Often these are stored in a file named signals.py
in the app
directory. But that is not required at all.
I have a signal file for an app. Where do I call it?
You load it in the AppConfig
which will load the module when the app is ready:
# my_app/apps.py
from django.apps import AppConfig
class MyAppConfig(AppConfig):
name = 'my_app'
verbose_name = 'My app name'
def ready(self):
import my_app.signals # noqa
Signals are often a bad idea. Indeed, signals can be circumventing, for example with a .bulk_create(…)
[Django-doc] that will not trigger signals for created objects. There are also a lot of scenarios where the data can change: creating records, updating records, removing records. It turns out that keeping the same data in sync, even on the same database, is not easy. I summarized some problems with signals in this article [django-antipatterns].