views:

39

answers:

1

I have a model that contains a boolean field representing the item's approval or not. I'd like to send an email when the box is checked.

I understand how to override the save method and send the email if it's true but this will send an email every time it's saved.

As I only want to send the email once, is there a way to check a boolean is true only for the first time?

Thanks

+3  A: 

What I do is that I get the element as it's in the database before saving and I compare it to what I have after.

def save(self):
    # Only when we update an element. Not when we create it
    if self.pk:
        # We get the old values of the model
        old = Model.objects.get(pk=self.pk)
        # If it's approved and it wasn't before
        if self.approved == True and old.approved == False:
            send_mail(...)
    super(Model, self).save()

So the email will be send only when the object goes from not approved to approved.

Damien MATHIEU
Thanks! I was pretty sure it was something like that but I couldn't figure out how to get the presave value in order to compare. Obvious once you see it, as always.
Colin