Django的。如何在管理站点中创建发送电子邮件的特定功能?

时间:2012-09-24 19:33:57

标签: python django django-models django-admin

如何在管理站点中创建特定功能,以便从我的模型向人(电子邮件)发送包含信息(名称,价格)的电子邮件?

这样的事情,但“向选定的人发送电子邮件”:

enter image description here

models.py

from django.db import models

class People(models.Model):
    name = models.CharField(max_length=255)
    price = models.DecimalField(max_digits=5, decimal_places=2)
    email = models.EmailField()

    def __unicode__(self):
        return self.name

admin.py

from django.contrib import admin
from demo.app.models import People

class PeopleAdmin(admin.ModelAdmin):
    list_display = ('name', 'email')

admin.site.register(People, PeopleAdmin)

电子邮件

send_mail('People information', 'People name is People.name, People price is People.price', 'mymail@gmail.com',
    ['email_from_email_field_from_model'])

1 个答案:

答案 0 :(得分:5)

你需要创建一个函数,并制作它。

def send_mail(modeladmin, request, queryset):
    for msg in queryset:
        send_mail('People information', msg.name, msg.price, msg.email,
    [msg.email])
send_mail.short_description = u'Send e-mail to selected objs'

class PeopleAdmin(admin.ModelAdmin):
    list_display = ('name', 'email')
    actions = (send_mail,)