Django signals using decorators

I have the following structure in my project

-parentDirectory
 -myModule
   __init__.py
   models.py
 handler.py

      


models.py

@register
class Student():
    number = models.PositiveIntegerNumber(unique=True)
    name = models.CharField(max_length=64)

def post_delete:
    """some code"""

def post_save:
    """some code"""

@register
class Course(SomeBaseModel):
    code = models.CharField(unique=True)
    teacher = models.CharField(max_length=64)

      

SomeBaseModel

has functions post_delete

and post_save

defined in it.

handler.py

def register(cls_object):
    @receiver(post_save, sender=cls_object.__name__)
    @receiver(post_delete, sender=cls_object.__name__)
    def _handler(sender, **kwargs):
        print 'inside _handler'

post_save.connect(_handler, sender=cls_object.__name__)
post_delete.connect(_handler, sender=cls_object.__name__)
return cls_object

      

_handler

fails when I remove any of the models. What am I missing?

0


source to share





All Articles