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.