Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to put django signal receiver code?

Tags:

django

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

like image 602
flexxxit Avatar asked Aug 11 '12 06:08

flexxxit


People also ask

Where is Pre_save signal in Django?

pre_save. This is sent at the beginning of a model's save() method. Arguments sent with this signal: sender.

What is the use of the Post_delete signal in Django?

Django Signals - post_delete()To notify another part of the application after the delete event of an object happens, you can use the post_delete signal.

What is Post_save in Django?

In the example above, save_profile is our receiver function, User is the sender and post_save is the signal. You can read it as: Everytime when a User instance finalize the execution of its save method, the save_profile function will be executed. If you supress the sender argument like this: post_save.


1 Answers

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'
like image 167
A Khalili Avatar answered Sep 18 '22 13:09

A Khalili