Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Location of signals in Django

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?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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].

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading