Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
732 views
in Technique[技术] by (71.8m points)

python - Failed to render template <Template memory:7f71d4d57a90> Odoo 13

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You got an:

UndefinedError: 'odoo.api.pos.session object' has no attribute 'user'

To get the username use user_id instead.

Example:

object.user_id.name

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...