33π
β
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)
or
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:
-
From the Django source code:
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.
-
Difference between
@classmethod
and@staticmethod
: What is the difference between @staticmethod and @classmethod in Python?
π€K Z
Source:stackexchange.com