In Odoo, managing relational fields effectively requires understanding the concept of triplets (or tuples). These are commands used to interact with fields like One2many
and Many2many
. This blog will guide you through using triplets to add attachments to an email template in Odoo.
What Are Triplets in Odoo?

Triplets are tuples with three components that define specific actions for managing relational fields. These actions include creating, updating, deleting, and linking records. Here’s a quick overview of the most common triplets:
Tuple | Action | Purpose |
---|---|---|
(0, 0, values) | Create a new record. | Add new data to a relational field. |
(1, id, values) | Update an existing record. | Modify data in a relational field. |
(2, id, _) | Delete a record from the database. | Permanently remove a record. |
(4, id, _) | Link an existing record to a relationship. | Attach an existing record. |
(5, _, _) | Clear all records from a relationship. | Remove all related records. |
(6, _, ids) | Replace all records in a relationship. | Set a specific list of related records. |
In this blog, we’ll focus on the (4, id, _)
triplet to link an existing attachment to an email template.
Scenario: Attaching a Document to an Email Template
Imagine you have a business requirement to send an email with an attachment (e.g., an invoice or a report). In Odoo, you can use triplets to add attachments to an email template.
Step 1: Create or Identify the Attachment
Attachments in Odoo are stored in the ir.attachment
model. To attach a file to an email template, you need the id
of the attachment. You can either create a new attachment or use an existing one.
Create an Attachment
attachment = self.env['ir.attachment'].create({
'name': 'Invoice Report',
'datas': base64.b64encode(b'Your file content goes here'),
'mimetype': 'application/pdf',
'res_model': 'res.partner',
'res_id': partner_id, # ID of the record this attachment is linked to
})
Step 2: Add the Attachment to an Email Template
To attach this file to an email template, we use the attachment_ids
field of the mail.template
model. This field is a Many2many
relationship, which allows us to link multiple attachments using triplets.
Example: Using the (4, id, _)
Triplet
email_template = self.env['mail.template'].browse(template_id) # Load the email template
email_template.write({
'attachment_ids': [(4, attachment.id, 0)], # Link the attachment
})
In this example:
(4, attachment.id, 0)
links the attachment with theid
obtained earlier to the email template.- The attachment will now be included when this email template is used to send emails.
Step 3: Send the Email
Once the attachment is linked, you can send the email using the template.
Example: Sending the Email
email_template.send_mail(record_id, force_send=True)
Here:
record_id
is the ID of the record the email is being sent to (e.g., a partner or invoice).force_send=True
ensures the email is sent immediately.
Full Example
Below is a complete example of adding an attachment to an email template and sending the email:
from odoo import models, api
import base64
class EmailAttachmentExample(models.Model):
_name = 'email.attachment.example'
@api.model
def add_attachment_and_send_email(self, partner_id, template_id):
# Step 1: Create the attachment
attachment = self.env['ir.attachment'].create({
'name': 'Invoice Report',
'datas': base64.b64encode(b'Your file content goes here'),
'mimetype': 'application/pdf',
'res_model': 'res.partner',
'res_id': partner_id,
})
# Step 2: Link the attachment to the email template
email_template = self.env['mail.template'].browse(template_id)
email_template.write({
'attachment_ids': [(4, attachment.id, 0)],
})
# Step 3: Send the email
email_template.send_mail(partner_id, force_send=True)
In the context of Odoo triplets, the 0
in [(4, attachment.id, 0)]
is a placeholder that isn’t used in the (4, id, _)
operation. Its inclusion is required to maintain the tuple structure for consistency across all triplet operations.
Why Is 0
There?
Odoo’s ORM expects triplets to always have three elements, even if one of them isn’t relevant to the specific operation. For the (4, id, _)
operation:
- The
4
indicates linking an existing record (in this case, linking their.attachment
record to a Many2many field). - The
id
is the record’s ID to link (e.g.,attachment.id
). - The
_
(represented as0
) is unused for this operation but must exist to fulfill the tuple’s structure.
Key Points
- The
0
in(4, attachment.id, 0)
has no functional impact. - It acts as a placeholder to ensure the tuple adheres to the required structure.
This design helps maintain consistency across operations like (0, 0, values)
or (6, 0, ids)
, where all three elements are actively used.
Key Benefits of Using Triplets
- Flexibility: Triplets allow you to manage relational fields dynamically.
- Reusability: You can easily link existing records instead of creating new ones.
- Efficiency: Operations like attaching files are seamless and require minimal code.
Understanding Odoo’s triplets is essential for efficient relational data management. The (4, id, _)
triplet is especially useful when adding attachments to email templates. By mastering these commands, you can handle complex scenarios with ease and flexibility.you migh also like Getting Started with the Odoo