Passing pre_delete () or post_delete () signal arguments?

I use signals to perform an action after deleting an object; however, sometimes I want to perform a different action (and not by default) depending on the degree of compression.

Is there a way to pass an argument to my signal? Or will I have to abandon the signal, but instead the hard code, what do 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 a better approach?

+3
source share
1 answer

, - . delete(), send_email , , post_delete() - .

- : ( , !!!)

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)
    ...

: , .

+4

Source: https://habr.com/ru/post/1721388/


All Articles