16

I'm using Django's post_save signal to send emails to users whenever a new article is added to the site. However, users still receive new emails whenever I use save() method for already created articles. How is it possible to receive emails only when NEW entry was added?

Thanks in advance

3 Answers3

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')
Rob Cowie
  • 22,259
  • 6
  • 62
  • 56
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.

Iain Shelvington
  • 31,030
  • 3
  • 31
  • 50