Django Send_Mail 值错误

发布于 2025-01-08 19:41:24 字数 1353 浏览 1 评论 0原文

我正在努力使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情徒 2025-01-15 19:41:24

可能有更好的方法,但你可以尝试以下方法:

list = []
for customer in Customer.objects.filter(group__name='group_two').values_list('email'):
    list.append(customer.email)

send_mail('<Subject>', '<Message>', '[email protected]', list, fail_silently=False)

There may be a better way, but you can try this:

list = []
for customer in Customer.objects.filter(group__name='group_two').values_list('email'):
    list.append(customer.email)

send_mail('<Subject>', '<Message>', '[email protected]', list, fail_silently=False)
各自安好 2025-01-15 19:41:24
[{email: u'[email protected]'}, {email: u'[email protected]'}]

看起来你检查了这个列表:

list(Customer.objects.filter(group__name='group_two').values('email'))

使用values_list:

list(Customer.objects.filter(group__name='group_two').values_list('email'))
...
[(u'[email protected]',), (u'[email protected]',)]

并使用flat=True的values_list:

list(Customer.objects.filter(group__name='group_two').values_list('email', flat=True))
...
[u'[email protected]', u'[email protected]']

检查文档
https://docs。 djangoproject.com/en/dev/ref/models/querysets/#django.db.models.query.QuerySet.values_list

[{email: u'[email protected]'}, {email: u'[email protected]'}]

it looks like you check this list:

list(Customer.objects.filter(group__name='group_two').values('email'))

with values_list:

list(Customer.objects.filter(group__name='group_two').values_list('email'))
...
[(u'[email protected]',), (u'[email protected]',)]

and for values_list with flat=True:

list(Customer.objects.filter(group__name='group_two').values_list('email', flat=True))
...
[u'[email protected]', u'[email protected]']

check documentation
https://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db.models.query.QuerySet.values_list

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文