Django - 发送有关模型更改的电子邮件 [英] Django - send email on model change

查看:19
本文介绍了Django - 发送有关模型更改的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在模型中的特定字段发生更改时发送电子邮件.是否可以?这就是我要找的.我有一个包含 BooleanField 的配置文件模型,当管理员选择为 true 时,我想向用户发送电子邮件.我知道我可以将它放在def save(self):"中,但是,只要模型更改并且字段为真,就会触发电子邮件.如果该字段从 False 更改为 True,有没有办法让它只发送电子邮件?

I want to send an email when a specific field is changed in a model. Is it possible? Here is what I am looking for. I have a profile model that includes a BooleanField that when the administrator selects to be true I want to send user an email. I know I could put it in a "def save(self):" but, that fires off an email anytime the model is changed and the field is true. Is there a way to have it only email if the field was changed from False to True?

推荐答案

save 方法非常适合您想要做的事情:

save method is a perfectly good place for what you want to do:

def save(self):
    if self.id:
        old_foo = Foo.objects.get(pk=self.id)
        if old_foo.YourBooleanField == False and self.YourBooleanField == True:
            send_email()
    super(Foo, self).save()

这篇关于Django - 发送有关模型更改的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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