如何在管理站点中创建特定功能,以便从我的模型向人(电子邮件)发送包含信息(名称,价格)的电子邮件?
这样的事情,但“向选定的人发送电子邮件”:
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'])
答案 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,)