0

Hey I apologize in advance for my English.

I use Django Signal in my models.py to delete user images from my media directory. Everything works great but in Django documentation I have found something like this:

Where should this code live?

Strictly speaking, signal handling and registration code can live anywhere you like, although it’s recommended to avoid the application’s root module and its models module to minimize side-effects of importing code.

In practice, signal handlers are usually defined in a signals submodule of the application they relate to. Signal receivers are connected in the ready() method of your application configuration class. If you’re using the receiver() decorator, import the signals submodule inside ready().

Soo my questions is where and how I should properly use Django signals and what kind of consequences may be if I still used Singals In my models.py

I also have found this topic: Where should signal handlers live in a django project? but it was for django 1.7 and may be a little out dated.

Matix
  • 25
  • 4
  • There are no consequences. Most projects simply define this in `signals.py` and load it in the app config, but that is more *conventional*. – Willem Van Onsem Nov 24 '20 at 18:19
  • That being said, one should be careful with signals, a lot of Django ORM calls, especially the ones that do processing in bulk circumvent the signals. – Willem Van Onsem Nov 24 '20 at 18:20
  • Ok so to be clearly. My signals will be work properly but best practice in Django say that I should put my signals into signals.py and load it in the app config. Am I right? – Matix Nov 24 '20 at 18:29
  • 2
    indeed, that's correct. – Willem Van Onsem Nov 24 '20 at 18:29

0 Answers0