如何根据通过Django生成的新主键发送电子邮件?

时间:2013-07-24 19:09:48

标签: python django email key

我想在Django中生成新的主键时向用户发送电子邮件。我在 Models.py:

中的代码
class PurchaseOrder(models.Model):
    product = models.CharField(max_length=256)
    vendor = models.CharField(max_length=256)
    dollar_amount = models.FloatField()
    item_number = models.AutoField(primary_key=True)

我已将其编码到每次刷新页面时都可以发送电子邮件

email = EmailMessage('Purchase Order System', 'your message here', to=['youremail@gmail.com'])
email.send()

但是,如何才能生成新的主键,发送电子邮件?

3 个答案:

答案 0 :(得分:3)

使用django signals

from django.db.models import signals

def send_email_on_new_order(instance, created, raw, **kwargs):
    # Fixtures or updates are not interesting.
    if not created or raw:
        return

    # `instance` is your PurchaseOrder instance.
    email = EmailMessage('Purchase Order System', 'your message here', to=['youremail@gmail.com'])
    email.send()

signals.post_save.connect(send_email_on_new_order, sender=PurchaseOrder, dispatch_uid='send_email_on_new_order')

您必须将此代码粘贴到加载django期间执行的某个位置,例如models.py

示例models.py

from django.core.mail import EmailMessage
from django.db import models
from django.db.models import signals

class PurchaseOrder(models.Model):
    product = models.CharField(max_length=256)
    vendor = models.CharField(max_length=256)
    dollar_amount = models.FloatField()
    item_number = models.AutoField(primary_key=True)

def send_email_on_new_order(instance, created, raw, **kwargs):
    # Fixtures or updates are not interesting.
    if not created or raw:
        return

    # `instance` is your PurchaseOrder instance.
    email = EmailMessage('Purchase Order System', 'your message here', to=['youremail@gmail.com'])
    email.send()

signals.post_save.connect(send_email_on_new_order, sender=PurchaseOrder, dispatch_uid='send_email_on_new_order')

答案 1 :(得分:1)

收听post_save信号。

from django.db.models import signals
from django.dispatch import receiver

@receiver(signals.post_save, sender=PurchaseOrder)
def email_handler(sender, **kwargs):
    if kwargs['created']:
        email = EmailMessage('Purchase Order System', 'your message here', to=['youremail@gmail.com'])
        email.send()

或者在模型中定义自定义save方法。

class PurchaseOrder(models.Model):
    def save(self, *args, **kwargs):
        # Check if this is a new key
        if not self.pk:
            email = EmailMessage('Purchase Order System', 'your message here', to=['youremail@gmail.com'])
            email.send()
        super(PurchaseOrder, self).save(*args, **kwargs)

答案 2 :(得分:1)

@voithos是正确的,这个自定义保存是解决问题的好方法。但是,根据对该问题的严格回答,在创建主键之前,电子邮件在技术上仅发送 。这可能无关紧要,但如果确实如此,我会按照以下内容订购代码:

def save(self, *args, **kwargs):
    email = False  # Don't email by default
    # Check if this is a new key
    if not self.pk:
        email = EmailMessage('Purchase Order System', 'your message here', to=['youremail@gmail.com'])
    super(PurchaseOrder, self).save(*args, **kwargs)
    if email:  # Email was created, and we have a new Primary Key, so send now.
        email.send()

注意:如果你需要一种方法来引用新创建的实例(例如,电子邮件需要包含一个包含刚刚创建的主键的链接),我个人会采用@kroolik提供的信号方法。< / p>