What Are the Special Command Operation for One2many & Many2many Fields
CelineGeorge1
756 views
17 slides
Aug 02, 2024
Slide 1 of 17
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
About This Presentation
In Odoo, managing One2many and Many2many fields involves using special command operations to handle related records efficiently. These operations allow developers to create, update, delete, and link records seamlessly.
Size: 229.23 KB
Language: en
Added: Aug 02, 2024
Slides: 17 pages
Slide Content
What Are the Special Command Operation for One2many & Many2many Fields Enterprise
Introduction Enterpr ise In Odoo, managing One2many and Many2many fields involves using special command operations to handle related records efficiently. These operations allow developers to create, update, delete, and link records seamlessly.
Special commands Enterprise These commands streamline the process of managing complex relationships between records, ensuring that data integrity and relational consistency are maintained.
Special commands Enterprise In Odoo, special command operations for One2many and Many2many fields facilitate efficient management of related records (0, 0, {values}): Creates a new record with the specified values. (1, id, {values}): Updates an existing record with the given ID using the specified values. (2, id, 0): Deletes the record with the given ID from the database.
Special commands Enterprise (3, id, 0): Unlinks the record with the given ID from the relation without deleting it from the database (applicable only for Many2many fields). (4, id, 0): Links an existing record with the given ID to the relation (applicable only for Many2many fields). (5, 0, 0): Clears all linked records (applicable only for Many2many fields). (6, 0, [ids]): Replaces all linked records with the list of IDs provided (applicable only for Many2many fields).
Explanation Enterprise Each command consists of three parts. The first part is a number that tells the command's type. The second part is the ID of the related record for commands like update or delete, or 0 for commands like create. The third part depends on the command: for delete or update, it's usually 0; for create, it's the values to be written on the record; and for set, it's the list of new IDs for related records.
Special Commands Enterprise CREATE = 0 UPDATE = 1 DELETE = 2 UNLINK = 3 LINK = 4 CLEAR = 5 SET = 6
CREATE Enterprise The create command is primarily employed for record creation. self.update({ 'line': [(fields.Command.create(data))] }) 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
Explanation Enterprise fields.Command.create(data): This part creates a command to create new records in the related model [(fields.Command.create(data))]: This wraps the command to create new records in a list. This is because Odoo expects a list of commands when updating a One2many or Many2many field. self.update({ ' line': ...}): This updates the line field of the current record (self) with the list of commands to create new records.
2. UPDATE Enterprise self.update({ 'line': [(fields.Command.update(id, data))] }) Explanation update(id, data): This part creates a command to update an existing record within the line field. The id is the identifier of the existing record that we want to update, and data is a dictionary of field-value pairs representing the new values for that record. [(fields.Command.update(id, data))]: This wraps the update command in a list.
3. DELETE Enterprise Remove the related record from the database and remove its connection self.update({ 'order_line': [(fields.Command.delete(id)) ] }) Explanation delete(id): Delete an existing record within the line field. The id is the identifier of the record that we want to delete. [(fields.Command.delete(id))]: This wraps the delete command in a list. Odoo expects a list of commands when modifying One2many or Many2many fields.
4. UNLINK Enterprise Delete the connection between self and the associated record. self.update({ 'line': [(fields.Command.unlink(id))] }) Explanation unlink(id): This part creates a command to unlink an existing record within the line field. The id is the identifier of the record we want to unlink. [(fields.Command.unlink(id))]: This wraps the unlink command in a list. Odoo expects a list of commands when modifying One2many or Many2many fields.
5. LINK Enterprise Add a connection between self and the related record. self.update({ 'line': [(fields.Command.link(id))] }) Explanation link(id): This part creates a command to link an existing record to the order_line field. The id is the identifier of the record we want to link. [(fields.Command.link(id))]: This wraps the link command in a list. Odoo expects a list of commands when modifying One2many or Many2many fields.
6. CLEAR Enterprise Remove all records associated with the current record. self.update({ 'line': [(fields.Command.clear())] }) Explanation clear(): This part creates a command to clear all records from the order_line field. [(fields.Command.clear())]: This wraps the clear command in a list. Odoo expects a list of commands when modifying One2many or Many2many fields.
7. SET Enterprise self.update({ 'line': [(fields.Command.set(id))] }) Explanation fields.Command.set(id): This part creates a command to set the records of the line field to the specified records. The id is the identifier(s) of the record(s) want to set. [(fields.Command.set(id))]: This wraps the set command in a list.
Enterprise Conclusion Hence in conclusion , we can use the many special command operations to set, clear and manipulate data to fields and helps us to s treamline the process of managing complex relationships between records, such that data integrity and relational consistency are maintained and thus enables developers to work and manipulate data in odoo .
For More Info. Check our company website for related blogs and Odoo book. Check our YouTube channel for functional and technical videos in Odoo. Enterprise www.cybrosys.com