Hello everyone. Now I’m gonna explain you, how to override write method.
The write method is invoked everytime you click on the ‘Edit’ and ‘Save’ button in the view. Once the record is being created and then if you modify and you save record that time the write is being called. So the write method is called whenever the record is modified. The values with whitch it is being modified come in the vals argument.
Here we have a Students model with name=’Adam’ and age= ’10’, We want to update the ‘age’ field to 12:
Explanation of code
- @api.multi is for multiple records, where you can loop through it. It will be the current Recordset without the itiration. So we should use @api.multi to write method.
- The write method is similar to create method, it updates existing record(s). Which records to update is defined by the ids parameter.
The write method updates existing records. It takes a dictionary of values and applies to all of the ids you pass. This is an important point, if you pass a list of ids, the values will be written to all ids. If you want to update a single record, pass a list of one entry, if you want to do different updates to the records, you have to do multiple write calls. You can also manage related fields with a write.