Skip to content

Instantly share code, notes, and snippets.

Created August 30, 2011 21:58
Show Gist options
  • Save anonymous/1182197 to your computer and use it in GitHub Desktop.
Save anonymous/1182197 to your computer and use it in GitHub Desktop.
Avoid infinite signals when saving an object instance in Django's pre_save or post_save
from django.db.models.signals import post_save
from django.dispatch import receiver
# models definition here ...
@receiver(post_save, sender=MyModel)
def handler_that_saves_a_mymodel_instance(sender, instance, **kwargs):
# without this check the save() below causes infinite post_save signals
if not getattr(instance, 'processed', False):
instance.some_field = complex_calculation()
instance.processed = True
instance.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment