如何让 Django 信号调用模型方法? [英] How can I have a Django signal call a model method?

查看:28
本文介绍了如何让 Django 信号调用模型方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许现在已经晚了,但我不知道为什么这不起作用.当我有 post_save 信号调用通用函数时,它可以工作,但是当我有 post_save 信号从模型调用方法时,没有任何反应.这是有效的代码:

Maybe it's just late, but I cannot figure out why this isn't working. When I have a post_save signal call a generic function, it works, but when I have a post_save signal call a method from a model, nothing happens. Here is code that works:

class Revision(models.Model):
    # Model junk...

def send_email(sender, instance, created, **kwargs):
    if created:
        print "DO STUFF"

signals.post_save.connect(send_email, sender=Revision)

但这不起作用:

class Revision(models.Model):
    # Model junk...

    def send_email(sender, instance, created, **kwargs):
        if created:
            print "DO STUFF"

signals.post_save.connect(Revision.send_email, sender=Revision)

有没有好心人能阻止我把头撞到墙上?谢谢.

Is there a kind soul out there who will keep me from smashing my head into the wall? Thanks.

推荐答案

在我看来,第二个中的问题是您使用了无界方法 send_mail.如果你真的想从一个类中调用 send_mail,也许 <代码>@classmethod@staticmethod 会帮助你:

It seems to me that the problem in the second one is you are using an unbounded method send_mail. If you really want to call send_mail from within a class, maybe @classmethod or @staticmethod will help you out:

class Revision(models.Model):
    # Model junk...

    @classmethod
    def send_email(cls, sender, instance, created, **kwargs):
        if created:
            print "DO STUFF"

signals.post_save.connect(Revision.send_email, sender=Revision)

class Revision(models.Model):
    # Model junk...

    @staticmethod
    def send_email(sender, instance, created, **kwargs):
        if created:
            print "DO STUFF"

signals.post_save.connect(Revision.send_email, sender=Revision)

或者不使用这些装饰器,您可以传递有界实例方法:

Alternatively without using these decorators, you can pass the bounded instance method:

class Revision(models.Model):
# Model junk...

    def send_email(self, sender, instance, created, **kwargs):
        if created:
            print "DO STUFF"

signals.post_save.connect(Revision().send_email, sender=Revision)

<小时>

参考资料:


References:

  1. 来自 Django 源代码:

def connect(self, receiver, sender=None, weak=True, dispatch_uid=None):
    """
    Connect receiver to sender for signal.

    Arguments:

        receiver
            A function or an instance method which is to receive signals.
            Receivers must be hashable objects.

            If weak is True, then receiver must be weak-referencable (more
            precisely saferef.safeRef() must be able to create a reference
            to the receiver).

            Receivers must be able to accept keyword arguments.

            If receivers have a dispatch_uid attribute, the receiver will
            not be added if another receiver already exists with that
            dispatch_uid.

  • @classmethod@staticmethod 的区别:Python中@staticmethod和@classmethod有什么区别?

    这篇关于如何让 Django 信号调用模型方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆