Detecting new objects on post_save handler

Thu 03 April 2008 | tags: django

Reading the scarce documentation on Django’s signals there did not seem to be a way to detect on the post_save handler if the object is a new instance or an update of an existing instance.

Reading the source code, however, reveals that on django/db/ at the end of save() method of Model class, we see this snippet:

# Run any post-save hooks.
dispatcher.send(signal=signals.post_save, sender=self.__class__,
instance=self, created=(not record_exists), raw=raw)

So on your post_save hook all you have to do is accept an extra parameter called ‘created’ which will be True if the instance was just created or False if it is simply an update.


Although the documentation of Django is pretty good, and certainly much better than a lot of other Python web frameworks, its still far from complete.

I used to use Webware once upon a time, and the only documentation for the bloody thing was the source code.

Comments !