[Solved]-Django: passing variables from pre_save to post_save signals

10👍

You can store them as instance attributes.

@receiver(pre_save, sender=Activity)
def send_user_profile_analytics(sender, **kwargs):
    instance = kwargs['instance']
    instance._activity_completed_old_value = instance.is_completed

@receiver(post_save, sender=Activity)
def send_user_profile_analytics(sender, **kwargs):
    instance = kwargs['instance']     
    if instance.is_completed != instance._activity_completed_old_value:
        # send analytics

In this way you “send analytics” only if is_completed changes during save (that means that save doesn’t just store the value but makes some further elaboration).

If you want to perform an action when a field is changed during instance life-time (that is from its creation till the save) you should store the initial value during post_init (and not pre_save).

👤Don

Leave a comment