where to put django signal receiver code, spread them over multiple files?

我只是一个虾纸丫 提交于 2019-12-04 10:12:14

See the docs:
https://docs.djangoproject.com/en/1.8/topics/signals/#connecting-receiver-functions

it's common to put them in a separate signals.py file, perhaps one per module in your project, but you need to ensure that these files get imported so that your signal receivers get registered.

as detailed in the docs above, Django 1.7+ now has the AppConfig.ready mechanism for this kind of case

There's a good explanation here:
http://chriskief.com/2014/02/28/django-1-7-signals-appconfig/

(as an example of what you might do)

basically:

# myapp/__init__.py
default_app_config = 'myapp.apps.MyAppConfig'

and

# myapp/apps.py
from django.apps import AppConfig

class MyAppConfig(AppConfig):

    name = 'myapp'
    verbose_name = 'My App'

    def ready(self):

        # import signal handlers
        import myapp.signals.handlers
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!