tags:

views:

102

answers:

1

I am using signals to perform an action after an object has been deleted; however, sometimes I want to perform a different action (not the default one) depending on an arugment.

Is there a way to pass an argument to my signal catcher? Or will I have to abandon the signal and instead hard code what I want to do in the models ?

What I would like to do is something like this:

>>> MyModelInstance.delete()
    # default pre_delete() signal is run, in this case, an email is sent
>>> MyModelInstance.delete(send_email=False)
    # same signal is run, however, no email gets sent

Any ideas on the best approach?

+1  A: 

I don't think you need to hardcode your actions in the model - you can still use signals. But you will need to override delete() to at the very least accept the send_email parameter and - since I don't think you can pass extra parameters into post_delete() - trigger your own custom signal.

Something like this: (writing from memory, untested!!!)

import django.dispatch
your_signal = django.dispatch.Signal(providing_args=["send_email",])

def your_callback(sender, **kwargs):
    print send_email

your_signal.connect(your_callback)

class YourModel(models.Model):
    ...
    def delete(self, send_email=True):
        super(YourModel, self).delete()
        your_signal.send(sender=self, send_email=send_email)
    ...

Disclaimer: don't know if that is the best approach.

celopes
I was able to use this approach - not sure if it is best, either, but it works for me for the time! Thanks.
thornomad