通过管理站点修改Django CharField时发送电子邮件通知 [英] Send an e-mail notification when a Django CharField is modified via the admin site

查看:171
本文介绍了通过管理站点修改Django CharField时发送电子邮件通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CharField通常是空的。当该字段设置为非空值时,我想向所有管理员发送电子邮件通知(使用 mail_managers )。对此字段的更改只能通过管理站点进行。



我认为这可能是通过信号可以做到的,但是我看不到在< a href =http://docs.djangoproject.com/en/dev/topics/signals/ =nofollow>文档。任何想法?

解决方案

如果您不想更新,那么您需要在字段中添加一些历史记录再次,只是添加一个保存方法到你的模型类似于应该工作:



从django.db导入模型

  class Test(models.Model):
empty_field = models.CharField(max_length = 20,blank = True)
def save(self):
如果self.pk不是None:
orig = Test.objects.get(pk = self.pk)
如果orig.empty_field!= self.empty_field和len(self.empty_field)> 0:
mail_managers ...
super(Test,self).save()#调用realsave()方法

请参阅
http:/ /www.djangoproject.com/documentation/models/save_delete_hooks/

Django:保存时,如何检查字段是否已更改?


I have a CharField that is normally empty. I want to send out an e-mail notification to all managers (using mail_managers) when the field is set to a non-empty value. Changes to this field should only happen via the admin site.

I assumed this might be something I could do via signals but I do not see an appropriate signal listed in the documentation. Any ideas?

解决方案

You will need to add some history to the field if you don't want updates if the field is changed again but just adding a save method to your model similar to should work:

from django.db import models

class Test(models.Model):
    empty_field = models.CharField(max_length=20, blank=True)
    def save(self):
        if self.pk is not None:
            orig = Test.objects.get(pk=self.pk)
            if orig.empty_field != self.empty_field and len(self.empty_field) > 0:
                mail_managers ... 
        super(Test, self).save() # Call the "real" save() method                

See http://www.djangoproject.com/documentation/models/save_delete_hooks/ and Django: When saving, how can you check if a field has changed?

这篇关于通过管理站点修改Django CharField时发送电子邮件通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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