Skip to content

Instantly share code, notes, and snippets.

@pablorecio
Forked from anonymous/models.py
Created August 31, 2011 06:31
Show Gist options
  • Save pablorecio/1182942 to your computer and use it in GitHub Desktop.
Save pablorecio/1182942 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, created, **kwargs):
# without this check the save() below causes infinite post_save signals
if created:
instance.some_field = complex_calculation()
instance.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment