In Odoo, there are different types of fields, From these, Relational fields are one of the important types of fields. Relational fields are used to link the data from one model to another model and also they act as a bridge between two different models. There are three types of relational fields in Odoo, Many2one, One2many, and Many2many.
Here we discuss the One2many fields and, How we can pass value to the child lines of One2many fields and it provides an option to create the relation between the child model and multiple rows of the parent model. One2many is the inverse of the Many2one relational field.
When creating a record in Odoo, we can pass values to its One2Many or Many2Many child by using commands, and each command is a three elements tuple where the first element is an integer that identifies the command. The second element is the related record id to apply the command, and the third element is either the values to write on the record. The first integer elements in the commands represent the different methods such as 0 for create, 1 for the update, 2 for delete, 3 for unlink, 4 for the link, 5 for clear, and 6 for the set. Here we can discuss the different types of commands and their working.
1. CREATE
The command (0, 0, { values }) is used to create a new record in the co-model using values and link to the new record to the current record.
Here is an example of creating a new invoice in a custom module,
invoice = self.env[‘account.move'].create({
'move_type': 'out_invoice',
'invoice_date': datetime.now(),
'invoice_line_ids': (0, 0, {
'product_id': self.product_id,
'price_unit': self.price_unit,
}) ],
})
Here create a new record with the fields invoice date and with the child lines product_id and price_unit.
2. UPDATE
The command (1, ID, { values }) is used to update the linked record with ID and write values on it.
Here is an example of the UPDATE method,
payment.move_id.write({
'line_ids': [
(1, counterpart_lines.id, {
'debit' : 0.0,
'credit ' : 75.0,
'amount_currency': -75.0,
'partner_id': self.partner_b.id,
}),
3. DELETE
The command (2, ID, 0) will remove and delete the linked record with ID from the database and remove its relation with the existing records.
Here is an example of the DELETE method,
def test_with_unlink(self):
country2 = self.env['res.country'].create({'name': 'other country'})
company = self.env['res.company'].create({
'name': 'test company',
'child_ids': [
(0, 0, {'name': 'Child Company 1'}),
(0, 0, {'name': 'Child Company 2'}),
]
})
child_company = company.child_ids[0]
company.write({
'country_id': country2.id,
'child_ids': [(2, child_company.id)],
})
4. UNLINK
The command (3, ID, 0) will remove the relation between the existing record and the related record with ID.
Here is an example of the UNLINK method,
def action_quit(self):
self.ensure_one()
return self.write({'user_ids': [(3, self._uid)]})
5. LINK
The command (4, ID, 0) will add a relation between the related record and the existing record with the ID, and it will delete the relationship between two objects, but it will not delete the target object.
Here is an example of the LINK method,
def action_confirm(self):
account_move_data = self.env['account.move'].search(
[('id', '=', self.invoice_id.id)])
account_move_data.write({
'name': self.invoice_id.name,
'warranty_request_ids': [(4, self.id, {
})]
})
6. CLEAR
The command (5, 0, 0) will remove all the records from the relation with the existing record. It acts like executing the unlink command on every record.
Here is an example of the CLEAR method,
def onchange_sale_order(self):
self.update({
'invoice_line_ids': [(5, 0, 0)],
'line_ids': [(5, 0, 0)],
})
7. SET
The command (6, 0, [IDs]) will replace the current relations of existing records with the list of linked IDs, and it acts like an unlink command on every relation which is removed and then executes the link command on every relation.
Here is an example of the SET method,
def _default_user_ids(self):
return [(6, 0, [self._uid])]
We can pass values to the child of One2many or Many2Many fields at the time of creating a new record using different types of commands such as create, update, link, unlink, clear, and set as mentioned above.