In this blog, we will discuss special command operations for the One2Many and Many2Many fields. There are some special commands to manipulate the relation they implement.
Each command is internally a three-element tuple, with the first element being a mandatory integer that identifies the command. The second element is either the related record id to which the command should be applied (commands update, delete, unlink, and link) or 0 (commands create, clear, and set). The third element can be either 0 (commands delete, unlink, link, and clear), the values to be written on the record (commands create and update), or the new ids list of related records (command set).
* CREATE = 0
* UPDATE = 1
* DELETE = 2
* UNLINK = 3
* LINK = 4
* CLEAR = 5
* SET = 6
Command.create(values: dict)
By using values, create new records in the comodel, and link the created records to self.
self.update({
'order_line': [(fields.Command.create(data))]
})
In the above code order_line is the One2Many field in the self.
fields.Command.create is the special command to perform
Data is variable passes a dictionary of the value in each field of the One2manyfield
In the case of Many2many relation, a single new record is created in the comodel, and all records in self are linked to the new record.
In the case of One2many relation, the comodel creates one new record for each record in the self, ensuring that each record in the self is connected to exactly one of the new records.
Command.update(id: int, values: dict)
Update the linked record with the given values
self.update({
'order_line': [(fields.Command.update(id, data))]
})
In the above code, the orderline is the One2Many field in the self. fields.Command.update is the special command to perform.
‘“id” is the ID of the record that needs to be updated. Data is a variable that passes a dictionary of the value in each field of the One2Manyfield.
Command.delete(id: int)
Delete the related record from the database and remove its relation itself
self.update({
'order_line': [(fields.Command.delete(id)) ]
})
In the above code, orderline is the One2many field in the self. fields.Command.delete is the special command to perform.
Id is the id of the record that needs to be deleted
If the record is still linked to other records in the case of a Many2many relation, removing it from the database may be prevented.
Command.unlink(id: int)
Remove the link between self and the linked record.
self.update({
'order_line': [(fields.Command.unlink(id))]
})
In above code orderline is the One2many field in the self. fields.Command.unlink is the special command to perform.
Id is the id of record that needs to be unlinked.
If the inverse field is set to ondelete='cascade' in the case of a One2many relation, the given record is deleted from the database. Otherwise, the inverse field's value is set to False and the record is kept.
Command.link(id: int)
Add a link between self and the related record.
self.update({
'order_line': [(fields.Command.link(id))]
})
In above code orderline is the One2many field in the self.fields.Command.link is the special command to perform.
Id is the id of the record that needs to be linked.
Command.clear()
Unlink all records from the relation with the self.
self.update({
'order_line': [(fields.Command.clear())]
})
In above code, orderline is the One2many field in the self. fields.Command.clear is the special command to perform.
This code will remove link between all related records and self.
Command.set(ids: list)
Update the relation, like replacing the current relations of self by the given new ones. Like using unlink command on every existing relation and using link command on new relation
self.update({
'order_line': [(fields.Command.set(id))]
})
In above code, orderline is the One2many field in the self. fields.Command.set is the special command to perform.
Id is the id of the record that needs to be replaced in relation.