Python Django Signal

Posted on January 16, 2018 in misc

In this article I am going to highlight issue with Django Signal.

I find problem in signal was “sometimes signals in django are triggered twice”. I thought issue might be with environment. I googled and also search on Stack-overflow. later I come to know why it happens.

It’s whether the code that connects the signals is imported more than once.

it means at the time of import singnal gets connected and that failed to get what we want to achieve using signal.

Solution:

its very simple to avoid such things using dispatch_uid in method as suggested in django doc.

Also need to take care of what dispatch_uid we use.

for example. If I use time.time() as dispatch_uid then it will not works.

So we should choose a convention like my_app.models.function_name as Dmitry suggests, then the second time the module is imported, the signal will not be connected twice because the dispatch_uid has not changed. It's up to you not to reuse the same dispatch id to register different callback functions with the same signal.

Hope you enjoys by reading this!