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.
1. Fix the send_change_password_link Method
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.partneraligns the template's default context with your partner data, makingobjectpoint directly to the record you're sending the email for.
内容的提问来源于stack exchange,提问作者M.E.

