7

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.

jerrymouse
  • 16,964
  • 16
  • 76
  • 97

1 Answers1

11

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
Willian
  • 2,385
  • 15
  • 17