-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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