Django Admin,Call功能保存 [英] Django Admin, Call function on save

查看:144
本文介绍了Django Admin,Call功能保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Django网站上有一个处理博文的应用程序。当发布博客文章时,我想在第三方应用程序上安排一个通知,通知订阅者新帖子。



所以我想添加一个自定义函数当博客文章被保存在哪里时,我可以将这个API调用写入通讯服务。



如何做?试图查看文档,所有我可以找到的是管理操作,似乎不是我正在寻找的。

解决方案

p>有很多方法可以使用。



覆盖模型保存方法很简单,但每次保存模型时都会调用该方法。



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



如果它是特定于管理站点,在您的ModelAdmin中使用model_save()方法。 (我个人喜欢这种方法,因为它不会干扰你的模型)。



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



您还可以使用帖子保存信号,但保存方法似乎是首选(取决于您在做什么)<一个href =https://stackoverflow.com/questions/5597378/django-when-to-customize-save-vs-using-post-save-signal> Django:何时使用保存后信息自定义保存/ a>


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.

解决方案

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/1.8/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/1.8/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

这篇关于Django Admin,Call功能保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆