Django Send_Mail 值错误
我正在努力使用 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)
我想向特定组的 mailing_list 发送电子邮件。我可以通过以下方式访问它
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'[email protected]'}, {email: u'[email protected]'}]
有什么想法吗?谢谢你
。我已经看过这个 stackoverflow 问题,但它对我来说并没有真正的帮助
想通了
经过四个小时的代码修改,我终于明白了。
mailing_list = []
for contact in Customer.objects.filter(group__name='group_two'):
mailing_list.append(contact.email)
I am struggling to use a QuerySet as an recipient arguement for the send_mail function
I have this model:
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)
I want to email the mailing_list for a specific group. I can access this via
mailList = list(Customer.objects.filter(group__name='group_two').values_list('email'))
However when I put mailList in my send_mail function I get a
Value Error: need more than 1 value to unpack
When I look at the mailList variable it looks like
[{email: u'[email protected]'}, {email: u'[email protected]'}]
Any ideas? Thank you
PS. I've looked at this stackoverflow question already but its not really helpful to me
Figured It Out
After four hours of messing around with code I finally got it.
mailing_list = []
for contact in Customer.objects.filter(group__name='group_two'):
mailing_list.append(contact.email)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可能有更好的方法,但你可以尝试以下方法:
There may be a better way, but you can try this:
看起来你检查了这个列表:
使用values_list:
并使用flat=True的values_list:
检查文档
https://docs。 djangoproject.com/en/dev/ref/models/querysets/#django.db.models.query.QuerySet.values_list
it looks like you check this list:
with values_list:
and for values_list with flat=True:
check documentation
https://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db.models.query.QuerySet.values_list