I'm performing some persistence actions with the model's object when receiving a post_save
signal, which includes a call to save()
. Obviously the save()
call sends a post_save
signal again and I'm landing in a signal recursion.
Is there a way to prevent Django to send a post_save
signal on a specific save()
call? Or can I detect in my signal callback if a call was "looped"?
That didn't work
I tried to modify the model's object by adding a attribute, but it seems like django.db.models.base.Model.save_base
passes a "sanitized" object to the signal callback, which does not contain that attribute anymore:
def callback(sender, **kwargs):
instance = kwargs['instance']
if not hasattr(instance, 'no_signal'):
# (...) Perform actions
instance.no_signal = True
instance.save()
post_save.connect(callback, dispatch_uid='post_save_callback')
Background
The complete situation is a bite more complex, than just receiving the signal, modifying the object and persisting it. In fact I have two identical Django instances (on different servers) which exchange created or modified objects (via ØMQ) and persist it in their own database. I don't want to use any kind of DB synchronization, since the application needs to be deployable easily and should even work with sqlite.
Since this should work for all models, regardless if they were modified/created via a view or the admin app, I'd prefer finding a way to use the post_save
signal rather than introducing an own one, which needs to be triggered in various points in the project (including the User
model).