1

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

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

在此处输入图像描述

模型.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

管理员.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'])
4

1 回答 1

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,)
于 2012-09-24T19:59:55.097 回答