Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get previous value of a field in post_save signal

I have a post signal in django, where I need to access previous value of a field:

post_save.connect(callback_function_postsave, sender=Media)

I know ideally I should use pre_save for this:

pre_save.connect(callback_function_presave, sender=Media)

def callback_function_presave(sender, instance,*args,**kwargs):
try:
    old_value = sender.objects.get(pk=instance.pk).field
except sender.DoesNotExist:
    return

However, its imperative to get the old_value in post_signal, because based on it, I have to decide whether to make a 3rd party api call or not. I cant make the api call in pre_save as the api is using the same database and expects updated value committed.

One possible way I can think of is add the old_value to instance itself which can then be accessed by post_save:

def callback_function_presave(sender, instance,*args,**kwargs):
try:
    instance.old_value = sender.objects.get(pk=instance.pk).field
except sender.DoesNotExist:
    return

def callback_function_postsave(sender, instance,*args,**kwargs):
try:
    old_value = instance.old_value
except:
    print "This is a new entry"

Is there any better way to achieve this.

like image 322
jerrymouse Avatar asked Aug 07 '12 10:08

jerrymouse


1 Answers

Unfortunately the post_save signal doesn't give you the old values (post_save). So storing the old value on the model seems to be a good solution.

I would have written the pre_save like this:

def save_old_value(sender, instance, *args, **kwargs):
    if instance.id:
        instance.old_value = instance.__class__.objects.get(id=instance.id).old_value
like image 170
Willian Avatar answered Sep 23 '22 01:09

Willian