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