10

I have a app in my Django site that handles blog post. When a blog post is published I want to schedule a Newsletter on a third party application that informs the subscribers of the new post.

So I want to add a custom function to be called when the blog post is saved where I can write this API call to the newsletter service.

How to do this? Tried looking through the documentations and all I can find is Admin Actions which doesn't seem to be what I'm looking for.

Marcus Lind
  • 10,374
  • 7
  • 58
  • 112
  • 1
    Django Signals is what you are looking for. [Post save signal](https://docs.djangoproject.com/en/1.8/ref/signals/#django.db.models.signals.post_save) is pretty useful for you. – Mp0int May 15 '15 at 06:17
  • 1
    U can use save method in model. That is easy for your work. Within that def django huey can use for queue. def save(self): super(ModelName,self).save() – Aslam Khan May 15 '15 at 06:30

2 Answers2

16

There are a number of approaches you could use.

Override the model save method is simple, but will be called every time the model is saved.

https://docs.djangoproject.com/en/dev/ref/models/instances/#saving-objects

If it is specific to the admin site, in your ModelAdmin use the model_save() method. (I like this approach personally, as it won't interfere with your model).

https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.save_model

You could also use a post save signal, but save methods seem to be preferred (depending on what you are doing) Django: When to customize save vs using post-save signal

pyjavo
  • 1,598
  • 2
  • 23
  • 41
wobbily_col
  • 11,390
  • 12
  • 62
  • 86
2

You should definitely go for https://docs.djangoproject.com/en/1.8/ref/signals/#django.db.models.signals.post_save, which provides the complete functionality that you are looking for here.