使用EmailMultiAlternatives的Celery错误

时间:2014-01-31 21:47:39

标签: python django celery

我有一个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内置的发送电子邮件功能的包装器,将其转变为芹菜工作者任务。但是,当我尝试运行代码时,出现以下错误:

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

这发生在创建芹菜任务的上下文中。我不确定这里发生了什么。当我不使用EmailMultiAlternatives并简单地使用Django内置的send_mail(它也被django-celery-email包装为芹菜任务)时,我没有收到错误。思考?

1 个答案:

答案 0 :(得分:-1)

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