Question

this is my model

class Profile(models.Model):
    activate = models.BooleanField(default=False)

Now i want to do is , whenever some one from admin panel makes it true , an email will be sent to this particular user whose account is activated.

But i want to sent mail only when the value becomes true from false. if the value is already true i dont want to send any mail .

tried this thing with post save , but it sends email after every save action on Profile Model

Was it helpful?

Solution

Here the code, that will do it (used pre_save signal):

from django.db.models.signals import pre_save
from django.dispatch import receiver

@receiver(pre_save, sender=Profile)
def profile_changed(sender, instance, *args, **kwargs):
    if instance.activate:
        if not instance.pk:
            print "Send email to user here"
        else:
            activate_was = sender._default_manager.filter(pk=instance.pk)\
                .values("activate").get()["activate"]
            if activate_was != instance.activate:
                print "Send email to user here"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top