I want to send email to manager group in pos, and added it in the chatter. So, I added a template, and added code to send email in the python function. But, I got the following error:
Failed to render template <Template memory:7f71d4d57a90> using values {'format_date': <function MailTemplate._render_template.<locals>.<lambda> at 0x7f71d6146c80>, 'format_datetime': <function MailTemplate._render_template.<locals>.<lambda> at 0x7f71d6146e18>, 'format_amount': <function MailTemplate._render_template.<locals>.<lambda> at 0x7f71d6146f28>, 'format_duration': <function MailTemplate._render_template.<locals>.<lambda> at 0x7f71d6146ea0>, 'user': res.users(2,), 'ctx': {'lang': 'en_US', 'tz': False, 'uid': 2, 'allowed_company_ids': [1], 'active_model': 'pos.config', 'active_id': 1, 'active_ids': [1], 'safe': False}, 'object': pos.session(3,)}
UndefinedError: 'odoo.api.pos.session object' has no attribute 'user'
Python code:
class PosSession(models.Model):
_inherit = 'pos.session'
def _warning_balance_closing(self):
self.ensure_one()
context = dict(self._context)
context['session_id'] = self.id
template_id = self.env.ref('my_module.email_template')
managers = self.env.ref('point_of_sale.group_pos_manager')
users = managers.users
users = list(set(users))
if template_id:
for user in users:
template_id.send_mail(self.id, force_send=True, raise_exception=True,
email_values={'email_to': user.email})
return {
'name': _('Balance control'),
'view_type': 'form',
'view_mode': 'form',
'res_model': 'closing.balance.confirm.wizard',
'views': [(False, 'form')],
'type': 'ir.actions.act_window',
'context': context,
'target': 'new'
}
Template:
<record id="email_template" model="mail.template">
<field name="name">Template e-mail</field>
<field name="email_from">${(user.email or '')|safe}</field>
<field name="subject">Template e-mail</field>
<field name="lang">${object.lang}</field>
<field name="model_id" ref="model_pos_session"/>
<field name="auto_delete" eval="True"/>
<field name="body_html">
<![CDATA[
<p>Dear ${object.user.name or ''},
Regards,
</p>
]]>
</field>
</record>
What's wrong in my code ? Any help please ?
Thanks.
question from:
https://stackoverflow.com/questions/65915713/failed-to-render-template-template-memory7f71d4d57a90-odoo-13 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…