[Fixed]-Django signals for new entry only

34👍

The post_save signal receives a boolean created argument which indicates if the saved instance was created.

def my_callback(sender, **kwargs):
    if kwargs['created']:
        print('Instance is new')

3👍

Take a look at https://docs.djangoproject.com/en/dev/ref/signals/#django.db.models.signals.post_save

There actually is an argument passed with the signal “created: A boolean; True if a new record was created”.

I think, that should do the trick.

0👍

Checking for instance.id is a nice way of determining if the instance is “new”. This only works if you use ids that are auto-generated by your database.

Leave a comment