New way to catch django signals

During preparations for 1.0 release Django signaling system was refactored. Details on Backwards Incompatible Changes Page, corresponding changeset is 8223.

Well, the main reason of changes is speeding up. Announced “90% improvement in the speed of signal handling“.

The main question is how to update own code to be compatible with new way. We need to replace old style dispatching with new way of signal object creation (django.dispatch.Signal). Let see post and pre save model signal dispatch example.

We have Item model and signal handler add_mime_type declared before. Old style connection:

dispatcher.connect(add_mime_type, signal=models.signals.pre_save, sender=Item)

New way to connect model signals with handlers:

models.signals.pre_save.connect(add_mime_type, sender=Item)

Looks pretty simple. Also we need to change one thing in handler. It must be declared as accepting kwargs. For example:

def add_mime_type(instance, **kwargs):

The main changes are done behind the scenes. Now models.signals.pre_save is instance of django.dispatch.Signal instead of plain object().


Tags

Blogroll

Recent bookmarks