Signals recorded more than once in django1.1 testserver

I defined the signal handler function in the models.py file. At the bottom of this file, I use signals.post_save.connect(myhandler, sender=myclass) , as recommended in the docs at http://docs.djangoproject.com/en/dev/topics/signals/ .

However, when I start the test server, a simple print-print debugging shows that the models.py file is imported twice and (as far as I can tell), this leads to my signal handler being registered twice. This means that each action is processed twice, which is clearly not the intended behavior.

The first import, apparently, occurs during the model verification phase, and the second occurs correctly when the model itself is needed during the first request processed by the server.

Should I register my signal handlers elsewhere? Is this a bug on test server 1.1? Did I miss something?

+1
source share
1 answer

Signature for the connect method

 def connect(self, receiver, sender=None, weak=True, dispatch_uid=None) 

where dispatch_uid is the identifier used to uniquely identify a specific instance of the receiver. This is usually a string, although it may be something hashed. If the recipients have the dispatch_uid attribute, the receiver will not be added if another relay already exists with this dispatch_uid .

So you can specify dispatch_uid in your connect call to make sure this fixes the problem.

+4
source

All Articles