Odoo 10 - 在邮件模板中检索实例模型字段

Odoo 10 - Retrieving instance model fields in mail template

我有以下扩展模型:

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}。 headers 的其余部分已正确填充。有什么建议吗?

您想使用 XML 模板发送电子邮件。然后,我建议您使用 email.template 模型的 send_mail 方法来发送您的电子邮件(而不是 mail.mail 中的 send)。您可以在 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 ?