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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With