python - django send_mail 来自查询集

标签 python django

我正在尝试使用 django send_mail 方法。我遇到了两个问题,特别是将邮件地址从列表转换为 csv 以供收件人使用。第二个问题是从通知模型获取用户电子邮件列表。

我的模型

class notifications(models.Model):
    notID = models.AutoField(primary_key=True)
    notSubject = models.CharField(max_length=100)
    notBody = models.TextField()
    notDate =  models.DateField(auto_now_add=True)
    notRecepients =  models.ManyToManyField(UserProfile, related_name='recepients')
    notPoster = models.ForeignKey(UserProfile,related_name='notposter') 
    deleted = models.BooleanField()

    def __unicode__(self):
    return u'%s ' % ( self.notSubject)

class UserProfile(User):
    onames = models.CharField(max_length=30, blank=True)
    phoneNumber = models.CharField(max_length=15, blank=True)
    regNo = models.CharField(max_length=15)
        image = models.ImageField(max_length=100,upload_to='photos/%Y/%m/%d', blank=True, null=True, default='photos/2010/03/placeholder.jpg')
    course = models.CharField(max_length=30, blank=True, null=True)
    timezone = models.CharField(max_length=50, default='Africa/Nairobi')
    smsCom = models.BooleanField()
    mailCom = models.BooleanField()


class notifications_sms_mail(models.Model):
    notId = models.OneToOneField(notifications)
    sms = models.BooleanField()
    email = models.BooleanField()

我的看法

def notif_sms_mail(request):
    user = UserProfile.objects.get(pk=request.session['_auth_user_id'])
    mailform = notif_sms_mail_Form(request.POST)
    if mailform.is_valid():
        notid=request.POST['notId']
        mailform.save()
        notif = notifications.objects.get(deleted='0', notID = notid)
        rec = (UserProfile.objects.values('email')).filter(mailCom='1')

        mail = notifications_sms_mail.objects.get(email='1', notId=notid)
        if notif.notID == mail.notId:
            send_mail(notSubject, notBody, 'from ****@gmail.com',rec, fail_silently=False)

最佳答案

UserProfile.objects.filter(mailCom='1').values_list('email', flat=True)

关于python - django send_mail 来自查询集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2551353/

相关文章:

python - 计算*滚动* Pandas 系列的最大回撤

python - 如何列出python3中脚本使用的所有python包

django - 在 Heroku 中创建一个新目录

django - Django 管理媒体文件在 Google App Engine 上的位置

python - 如何为已经实例化的类 python 覆盖 __repr__ 方法

python - Twisted 在此平台上不受支持的轮子

python - 如何使用 OpenCV 和 Python 录制视频?

css - Django 表单 : set CSS class for list of choices

Django:检查 Field 对象中的 ManyToManyField 是否为空

python - __init__ 形式 Django 中的查询集