【问题标题】:Odoo 10 - Retrieving instance model fields in mail templateOdoo 10 - 在邮件模板中检索实例模型字段
【发布时间】:2018-03-21 00:38:44
【问题描述】:

我有以下扩展模型:

class ResPartner(models.Model):
    _inherit = 'res.partner'

    token = fields.Char('Change Password Token')

    @api.one
    def send_change_password_link(self):
            template = self.env.ref('extended_respartner.ecommerce_password_change')
            body = template.body_html
            receipt_list=[self.email]
            email_cc=[]
            email_from=self.company_id.email
            if template:
                    mail_values = {
                            'subject': template.subject,
                            'body_html': body,
                            'email_to':';'.join(map(lambda x: x, receipt_list)),
                            'email_cc':';'.join(map(lambda x: x, email_cc)),
                            'email_from': email_from,
                    }
                    create_and_send_email = self.env['mail.mail'].create(mail_values).send()

以及以下电子邮件模板:

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <record id="ecommerce_password_change" model="mail.template">
            <field name="name">Password Change</field>
            <field name="model_id" ref="base.model_res_partner"></field>
            <field name="lang">${object.lang}</field>
            <field name="auto_delete" eval="False"/>
            <field name="email_from">${object.company_id and object.company_id.email or ''}</field>
            <field name="reply_to">${object.company_id and object.company_id.email or ''}</field>
            <field name="email_to">${object.email}</field>
            <field name="subject">Change Password ${object.token}</field>
            <field name="body_html"><![CDATA[
                    <p>Dear Customer:</p>
                    <p>This is your token: ${object.token}</p>
            ]]></field>
    </record>
</odoo>

但它不会用它的值代替${object.token}。其余标题已正确填充。有什么建议吗?

【问题讨论】:

  • 如果要基于邮件模板发送邮件,为什么不使用email.template模型的send_mail方法,而不是mail.mail中的send呢?跨度>
  • @forvas 我使用来自email.templatesend_mail 并且它有效。我在互联网上关注了错误的信息

标签: python xml odoo odoo-10 qweb


【解决方案1】:

您想使用 XML 模板发送电子邮件。然后,我建议您使用email.template 模型的send_mail 方法来发送您的电子邮件(而不是来自mail.mailsend)。您可以在 mail 模块的 mail_template.py 文件中找到它。看看它的声明:

@api.multi
def send_mail(self, res_id, force_send=False, raise_exception=False, email_values=None):
    """Generates a new mail message for the given template and record,
        and schedules it for delivery through the ``mail`` module's scheduler.

        :param int res_id: id of the record to render the template with
                            (model is taken from the template)
        :param bool force_send: if True, the generated mail.message is
            immediately sent after being created, as if the scheduler
            was executed for this message only.
        :param dict email_values: if set, the generated mail.message is
            updated with given values dict
        :returns: id of the mail.message that was created
    """
    self.ensure_one()
    Mail = self.env['mail.mail']
    Attachment = self.env['ir.attachment']  # TDE FIXME: should remove dfeault_type from context

    # create a mail_mail based on values, without attachments
    values = self.generate_email(res_id)
    values['recipient_ids'] = [(4, pid) for pid in values.get('partner_ids', list())]
    values.update(email_values or {})
    attachment_ids = values.pop('attachment_ids', [])
    attachments = values.pop('attachments', [])
    # add a protection against void email_from
    if 'email_from' in values and not values.get('email_from'):
        values.pop('email_from')
    mail = Mail.create(values)

    # manage attachments
    for attachment in attachments:
        attachment_data = {
            'name': attachment[0],
            'datas_fname': attachment[0],
            'datas': attachment[1],
            'type': 'binary',
            'res_model': 'mail.message',
            'res_id': mail.mail_message_id.id,
        }
        attachment_ids.append(Attachment.create(attachment_data).id)
    if attachment_ids:
        values['attachment_ids'] = [(6, 0, attachment_ids)]
        mail.write({'attachment_ids': [(6, 0, attachment_ids)]})

    if force_send:
        mail.send(raise_exception=raise_exception)
    return mail.id  # TDE CLEANME: return mail + api.returns ?

【讨论】:

    猜你喜欢
    • 2019-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多