You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Odoo 10:如何在邮件模板中获取扩展ResPartner模型实例字段?

Got it, let's fix this step by step. To access your custom token field in the email template, you need to make two key adjustments: correctly render the template with your partner record in context, and then reference the field properly in the template itself.

Your current code pulls the raw template HTML without rendering it, which means template variables won't be replaced with actual values. Instead, use Odoo's render() method to process the template and pass your partner instance into the rendering context.

Here's the updated method:

@api.one
def send_change_password_link(self):
    template = self.env.ref('extended_respartner.ecommerce_password_change')
    if template:
        # Build context with the current partner record and other useful data
        render_context = {
            'object': self,
            'company': self.company_id,
            'user': self.env.user
        }
        # Render subject and body using the context to replace variables
        rendered_subject = template.render(render_context, engine='ir.qweb')
        rendered_body = template.render(render_context, engine='ir.qweb', field='body_html')
        
        mail_values = {
            'subject': rendered_subject,
            'body_html': rendered_body,
            'email_to': self.email,
            'email_cc': '',  # Add your CC list here if needed
            'email_from': self.company_id.email or self.env.user.email,
            'model': 'res.partner',
            'res_id': self.id,  # Link the email to the partner record for tracking
        }
        # Create and send the email
        self.env['mail.mail'].create(mail_values).send()

2. Reference the token Field in Your Email Template

First, in the Odoo backend, open your email template (extended_respartner.ecommerce_password_change) and make sure its Model is set to Contact (res.partner)—this ensures the default context uses the partner record as object.

Then, in the template's Body HTML section, use Odoo's QWeb syntax to access the token field:

<p>Hi {{ object.name }},</p>
<p>You requested to reset your password. Click the link below to proceed:</p>
<p>
  <a href="https://your-domain.com/reset-password?token={{ object.token }}">
    Reset My Password
  </a>
</p>
<p>If you didn't request this, feel free to ignore this email.</p>

Why This Works

  • The render() method processes the template and replaces QWeb variables (like {{ object.token }}) with actual values from the partner record you passed in the context.
  • Setting the template's model to res.partner aligns the template's default context with your partner data, making object point directly to the record you're sending the email for.

内容的提问来源于stack exchange,提问作者M.E.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:30:22