Home > Software design >  ValueError: Template rendering for language should be called with a list of IDs
ValueError: Template rendering for language should be called with a list of IDs

Time:05-11

I'm trying to send an email from the module, in Odoo 15 here is my method.

student.py

    def action_send_reply_by_email(self):
         template_id = self.env.ref('om_university.student_card_email_template').id
         template = self.env['mail.template'].browse(template_id)
         template.send_mail(self.ref, force_send=True)

and this is my template. mail_tamplate.py

<?xml version="1.0" encoding="UTF-8" ?>
<odoo>

<data noupdate="1">
    <!--Email template -->
    <record id="student_card_email_template" model="mail.template">
        <field name="name">Student card: Send by email</field>
        <field name="model_id" ref="om_university.model_university_student"/>
        <field name="email_from">{{ (object.invoice_user_id.email_formatted or 
               user.email_formatted) }}</field>
        <field name="email_to">{{ object.email }}</field>
        <field name="lang">${object.lang}</field>
        <field name="subject">Invoice (Ref {{ object.name or 'n/a' }})</field>
        <field name="body_html" type="html">
            <div style="margin: 0px; padding: 0px;">
                <p style="margin: 0px; padding: 0px; font-size: 13px;">
                    Dear
                    <br/><br/>
                    Here is your
                    <br/><br/>
                    Do not hesitate to contact us if you have any questions.
                 </p>
              </div>
          </field>
       </record>
   </data>

</odoo>

but when I click on the send button I get this error. ValueError: Template rendering for language should be called with a list of IDs

CodePudding user response:

According to the send_mail function definition, the first parameter should be an integer:

:param int res_id: id of the record to render the template

In the following example, I suppose ref is a many2one field:

def action_send_reply_by_email(self):
     template_id = self.env.ref('om_university.student_card_email_template')
     template_id.send_mail(self.ref.id, force_send=True)

You do not need to call the browse method to get the template record.

CodePudding user response:

I tried this code on the sudent.py module, and I delete the template mail_tamplate.py, the sending works for me.

  def action_send_reply_by_email(self):
    template_obj = self.env['mail.mail']
    template_data = {
        'subject': 'messege from the university of : '   self.university,
        'body_html': 'the messege here',
        'email_from': '[email protected]',
        'email_to': self.email
    }
    template_id = template_obj.create(template_data)
    template_obj.send(template_id)
  • Related