gpt4 book ai didi

python - EmailMultiAlternatives 的 Celery 错误

转载 作者:太空狗 更新时间:2023-10-30 00:19:58 24 4
gpt4 key购买 nike

我有一个围绕 EmailMultiAlternatives 的包装器,使界面更简洁(几乎逐字地取自 here):

class Email(object):
'''
Wrapper around the Django core's EmailMultiAlternatives that makes it simpler
to render txt and html templates.
'''

def __init__(self, to, subject):
self.to = to
self.subject = subject
self.html = None
self.text = None

def _render(self, template, context):
return render_to_string(template, context)

def render_html(self, template, context):
self.html = self._render(template, context)

def render_text(self, template, context):
self.text = self._render(template, context)

def send(self, from_addr=None, fail_silently=False):
if isinstance(self.to, basestring):
self.to = [self.to]
if not from_addr:
from_addr = settings.EMAIL_HOST_USER
msg = EmailMultiAlternatives(
self.subject,
self.text,
from_addr,
self.to
)
if self.html:
msg.attach_alternative(self.html, 'text/html')
msg.send(fail_silently)

我这样调用它:

   if not self.email:
warnings.warn('uid:%s has no email address' % self.id)
else:
context = Context({
'first_name': self.first_name,
'uid': int_to_base36(self.id),
'token': default_token_generator.make_token(self),
'domain': Site.objects.get_current().domain
})

from_email = settings.EMAIL_HOST_USER
subject = "Password Reset"
email = Email(to=self.email, subject=subject)
email.render_text('email/reset_password_email.txt', context)
email.render_html('email/reset_password_email.html', context)
email.send()

我正在使用 django-celery-email,它只是提供了一个围绕 Django 内置发送电子邮件功能的包装器,将其转换为 celery worker 任务。但是,当我尝试运行代码时,出现以下错误:

TypeError: <django.core.mail.message.EmailMultiAlternatives object at 0x10c20f3d0> is not JSON serializable

这发生在创建 celery 任务的上下文中。我不确定这里发生了什么。当我不使用 EmailMultiAlternatives 而只是使用 Django 的内置 send_mail(它也被 django-celery-email 包装为 celery 任务)时,我没有收到错误。想法?

最佳答案

您是否尝试从 EmailMultiAlternatives 继承您的 Email 类?通过这样做,您仍然可以实现用于 celery 的方法。

关于python - EmailMultiAlternatives 的 Celery 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21490664/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com