The write()
method in Odoo is used to update the values of records in a model. When working with a many2many
field, the write()
method can be used to add, remove, or replace the associated records in that field.
How to Use the write()
Method on a many2many
Field
A many2many
field represents a bi-directional relationship between two models where multiple records of one model can be associated with multiple records of another model. The write()
method on a many2many
field takes specific commands to manage these relationships. The commands used to manipulate many2many
fields are:
- Add (
(4, id)
): Adds a record with a specific ID to themany2many
field. - Remove (
(3, id)
): Removes a record with a specific ID from themany2many
field. - Replace (
(6, 0, [id1, id2, ...])
): Replaces all existing records in themany2many
field with the list of IDs provided. - Clear (
(5, )
): Clears all records from themany2many
field. - Create and Link (
(0, 0, {vals})
): Creates a new record with the values provided and links it to themany2many
field.
Practical Examples
Let’s assume we have the following models:
- Book Model (
library.book
) - Author Model (
library.author
)
The library.book
model has a many2many
field named author_ids
that links to the library.author
model.
class LibraryBook(models.Model):
_name = 'library.book'
_description = 'Book Record'
name = fields.Char(string='Title', required=True)
author_ids = fields.Many2many('library.author', string='Authors')
Now, let’s see how to use the write()
method to modify the many2many
field author_ids
:
1. Adding an Author to a Book
To add an existing author to a book, you can use the (4, id)
command:
# Find the book and author records
book = self.env['library.book'].browse(book_id)
author_id = 5 # Example author ID to be added
# Add the author to the book's author_ids field
book.write({'author_ids': [(4, author_id)]})
2. Removing an Author from a Book
To remove an author from a book, you can use the (3, id)
command:
# Find the book record
book = self.env['library.book'].browse(book_id)
author_id = 5 # Example author ID to be removed
# Remove the author from the book's author_ids field
book.write({'author_ids': [(3, author_id)]})
3. Replacing All Authors for a Book
To replace all existing authors with a new list of authors, use the (6, 0, [id1, id2, ...])
command:
# Find the book record
book = self.env['library.book'].browse(book_id)
new_author_ids = [1, 2, 3] # New list of author IDs
# Replace all existing authors with the new list
book.write({'author_ids': [(6, 0, new_author_ids)]})
4. Clearing All Authors from a Book
To remove all authors associated with a book, use the (5,)
command:
# Find the book record
book = self.env['library.book'].browse(book_id)
# Clear all authors from the book's author_ids field
book.write({'author_ids': [(5,)]})
5. Creating a New Author and Linking It to a Book
To create a new author and directly link it to a book, use the (0, 0, {vals})
command:
# Find the book record
book = self.env['library.book'].browse(book_id)
new_author_data = {'name': 'New Author Name'} # Data for the new author
# Create a new author and link it to the book
book.write({'author_ids': [(0, 0, new_author_data)]})
Conclusion
The simple write()
method in managed Odoo server solutions is utilized to update the values of previous records in any model. At the time of working with a many2many
field, this method can be specially used to add, delete, or replace the related records in that specific field.