2

每当在 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()

但是我怎样才能让它每次生成一个新的主键时,发送一封电子邮件?

4

3 回答 3

3

Use 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')

You have to paste this code somewhere it will be executed during loading django, e.g. models.py

Example 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')
于 2013-07-24T19:15:04.877 回答
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)
于 2013-07-24T19:11:48.567 回答
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 提供的信号方法。

于 2013-07-24T19:23:44.120 回答