[Django]-Django signals. how to create a unique dispatch id?

12👍

Using the time as a dispatch id won’t work. The issue isn’t whether your environment is multi-user or not. It’s whether the code that connects the signals is imported more than once.

Say your module was imported twice, 5 seconds apart. You have effectively done the following.

my_signal.connect(my_function, dispatch_uid=1332407342.51)
my_signal.connect(my_function, dispatch_uid=1332407352.51)

Your signal has been connected twice with different dispatch ids. This default project structure for Django 1.3 and earlier allows this double import to occur, as modules can often be imported as project.my_app.module and my_app.module.

If you 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 id has not changed. It’s up to you not to reuse the same dispatch id to register different callback functions with the same signal.

8👍

Just use string to the module, like 'apps.models.signal_name'

Leave a comment