Search code examples
djangodjango-signals

Django: how can I tell if the post_save signal triggers on a new object?


I need to do some background post-processing on newly created objects in Django. This post-processing should only run on new objects, not objects that are just updated.

I know that in pre_save I can check if the object has an id, if it has not then it's a new object. But the problem is that in the post-processing I need access to the id (so that I can save the results back to the database).

How can I do this in a clean way?


Solution

  • Have a look at docs: https://docs.djangoproject.com/en/stable/ref/signals/#post-save

    There is a created named argument which will be set to True if it's a new object.