Where to put the django signal receiver code?

Hi, I am defining my own signal and receiver. I just want to know where I should post my codes. It is located in models.py or in views.py. Please, help

+7
source share
3 answers
  • Create a python file - signals.py in your application.
  • Add this line to __init__.py in your application.

    import signals

  • Restart the django project.

Work!

0
source

You can create a signals.py file. But I can not add import signals to the __init__.py file. Because signal-related models will not be loaded yet. So, in the apps.py file you can find a class that inherits from the AppConfig class and this class has a ready(self) method. You must import your signals in this function:

 from django.apps import AppConfig class MainConfig(AppConfig): name = 'main' def ready(self): import main.signals 
0
source

Since you can have different ways to use your django code, for example, as a WSGI application (django.core.wsgi) or in hocking scripts with django.setup () call

a good idea is to put your signal code in a file with the model itself or import it into a file with a model

or the class of methods of the model itself, as suggested here by fooobar.com/questions/51488 / ...

-1
source

Source: https://habr.com/ru/post/922621/


All Articles