sender
here is the Message
class itself, not the instance that's being saved. The actual instance is passed as the keyword argument instance
. Also, with post_save
, if you're not careful, you'll get yourself in an infinite loop. Better to use pre_save
.
@receiver(pre_save, sender=Message)
def my_handler(sender, **kwargs):
instance = kwargs['instance']
if not instance.charge:
instance.charge = instance.length()
# No need to save, as we're slipping the value in
# before we hit the database.