Where should signal handlers live in a django proj

2019-01-04 05:40发布

I have just started implementing signal listeners in a django project. While I understand what they are and how to use them. I am having a hard time figuring out where I should put them. The documentation from the django site has this to say:

Where should this code live?

You can put signal handling and registration code anywhere you like. However, you'll need to make sure that the module it's in gets imported early on so that the signal handling gets registered before any signals need to be sent. This makes your app's models.py a good place to put registration of signal handlers.

While its a good suggestion, having non model classes or methods in my models.py just rubs me the wrong way.

So then, what is the best practice/rule for storing and registering signal handlers?

7条回答
▲ chillily
2楼-- · 2019-01-04 06:15

I actually like to make them classmethods of the model itself. That keeps everything within one class, and means you don't have to worry about importing anything.

查看更多
登录 后发表回答