Hi I am defining my own signal and receiver. I just want to know where I should place my codes. It is in models.py or in views.py. Please help
Asked
Active
Viewed 2,563 times
9
-
2Mostly in `models.py`, but if you want you can put that in separate file like `signal_handlers.py` – Rohan Aug 11 '12 at 06:22
-
I was able to find out. I Placed my objects in models.py and imported them in views where I used the sender function – flexxxit Aug 11 '12 at 07:00
-
You Know I tried defining my own sigs.py but the function call was not working. Can U help me out. i would really like to tidy up my code – flexxxit Aug 11 '12 at 07:05
-
Could you update your question with relevant error details. – Rohan Aug 11 '12 at 07:40
-
I created the signals_handlers.py and it works fine. thanks – flexxxit Aug 11 '12 at 07:43
-
Hmmm, that happened 2nd time with me today! – Rohan Aug 11 '12 at 07:45
-
I usually put the hooks - i.e. `post_save.connect(...)` - in my `models.py` and the handlers themselves in a separate `signals.py` file. That way they are separate, imported automatically and don't clog up your models with extra code. – Timmy O'Mahony Aug 11 '12 at 14:14
-
possible duplicate of [Where should signal handlers live in a django project?](http://stackoverflow.com/questions/2719038/where-should-signal-handlers-live-in-a-django-project) – guettli May 06 '14 at 08:03
-
this question has already been answered here: http://stackoverflow.com/questions/7115097/the-right-place-to-keep-my-signals-py-files-in-django – matias elgart Nov 11 '16 at 14:40
4 Answers
4
Put your signal function into signals.py
and
don't forget to put the app config in the __init__.py
file and also
ready method in the apps.py
file.
apps.py
:
from django.apps import AppConfig
class AppNameConfig(AppConfig):
name = 'app_name'
def ready(self):
import app_name.signals
__init__.py
(app folder):
default_app_config = 'app_name.apps.AppNameConfig'
0
- Create python file -
signals.py
in your application. Add this line in
__init__.py
in your application.import signals
Restart django project.
Work it!

makketagg
- 216
- 1
- 4
0
You can create signals.py
file. But can not add import signals
to __init__.py
file. Because signal related models will not be loaded yet. So, in apps.py file you can find class which inherit from AppConfig
class and this class has method ready(self)
method. You should import your signals in this function:
from django.apps import AppConfig
class MainConfig(AppConfig):
name = 'main'
def ready(self):
import main.signals

Tolqinbek Isoqov
- 168
- 8
0
Given an app called "app" to handle the pre_save signal of a model called "Model"
apps.py
from django import apps
from django.db.models import signals
class AppConfig(apps.AppConfig):
name = 'app'
def ready(self):
# import here to avoid circular imports
from app import signals as app_signals
signals.pre_save.connect(app_signals.model_pre_save, sender=self.get_model('Model'))
signals.py
def model_pre_save(sender, instance, **kwargs):
if kwargs.get('raw', False):
return # raw implies fixture, ignore
# add code here to handle signal

Airith
- 2,024
- 1
- 14
- 10