I wanted to get some feedback on a patch [1] I wrote for #9015. I am on board with the notion decorators can be used for registration patterns. Recently, I've been using signals a bit more which has spiked my interest in this ticket. Since Django 1.2 has a Python 2.4 minimum requirement and we're before the feature freeze I figured I'd take a stab at it.
The approach I took was slightly different than what zvoase took in his patches. I felt that using the connect method of Signal would be risky business. It is stable API that I didn't want to go modifying. Also, the idea of the decorator is to only connect which makes other methods on the signal useless. I appreciate the work zvoase did though, so thanks!
My approach introduces new API @receiver from django.dispatch. It takes the signal and kwargs and calls connect appropriately. Here's a quick example:
from django.db.models.signals import pre_save
from django.dispatch import receiver
@receiver(pre_save, sender=MyModel)
def my_receiver(sender, **kwargs):
...
Any thoughts or objections to committing?
[1]: http://github.com/brosner/django-svn/commit/970dcae959af16699b78ebca13ac878a6ffe86a7
Am 23.01.2010 um 14:01 schrieb Brian Rosner:
>
> from django.db.models.signals import pre_save
> from django.dispatch import receiver
>
> @receiver(pre_save, sender=MyModel)
> def my_receiver(sender, **kwargs):
> ...
>
Just an idea:
Wouldn't it be a bit nicer API to have receiver be a method on Signal?
So you could do:
@pre_save.receiver(sender=MyModel)
def my_receiver(sender, **kwargs):
...
Bye
Ulrich
This isn't a bad idea. It does reduce an import and does help make it more clear than using connect. I'd be in favor of changing it to this.
Brian Rosner
http://oebfare.com
http://twitter.com/brosner