【发布时间】:2012-03-14 07:48:03
【问题描述】:
我正在努力使用 QuerySet 作为 send_mail 函数的收件人参数
我有这个模型:
class Group(models.Model):
name = models.CharField(primary_key=True)
mailing_list = models.ManyToManyField("Customer", null=True)
class Customer(models.Model):
name = models.CharField()
email = models.EmailField(primary_key=True)
我想通过电子邮件发送特定组的邮件列表。我可以通过
mailList = list(Customer.objects.filter(group__name='group_two').values_list('email'))
但是,当我将 mailList 放入我的 send_mail 函数时,我得到一个
Value Error: need more than 1 value to unpack
当我查看 mailList 变量时,它看起来像
[{email: u'someonesname@domain.com'}, {email: u'anothername@domain.com'}]
有什么想法吗?谢谢
PS。我已经看过this stackoverflow question,但它对我没有帮助
想通了
在搞了四个小时的代码后,我终于搞定了。
mailing_list = []
for contact in Customer.objects.filter(group__name='group_two'):
mailing_list.append(contact.email)
【问题讨论】:
-
这可能一步到位,但未经测试:
mailing_list = Customer.objects.filter(group__name='group_two').values_list('email', flat=True)
标签: django email model django-queryset