Updating Records
Occasionally, there might be a need to modify records by updating the values of certain fields. This can be achieved using the update() method of a model. Assigning a field value to a record can be done through one of three methods.
1. Directly assigning values to the attribute that corresponds to the field of the record.
-Directly assigning a value to a field is possible.
self.test_boolean = True
The 'self' represents the object of the record for which the field value, 'test_boolean' in this case, is being updated.
2.update() method
-Utilizing the update() method allows us to alter the value of a field.
self.update({
'start_date': fields.Date.today(),
'test_boolean': True
})
The 'start_date' and 'test_boolean' are the fields we intend to update. By providing a dictionary to the update method, we can map the field names to the desired values for modification.
3.write() method
The update method can be supplied with a dictionary that links the field names to the values intended for setting.
self.write({
'start_date': fields.Date.today(),
'test_boolean': True
})
The final two options necessitate a separate database call for each record and each field. In contrast, this approach updates all records with the designated values in a single database operation, suitable for recordsets of any size. However, it comes with certain limitations: it doesn't function if the records are not yet in the database. Additionally, when writing relational fields, it demands a specific format akin to the one utilized by the create() method.
fields.command.create({ values }) – Establish a connection to a new record that necessitates creation, utilizing the provided dictionary of values.
fields.command.update(ID, { values }) – Modify the associated record with the identifier (ID) by updating it with the specified values.
fields.command.delete( ID ) – Eliminate and erase the linked record identified by ID (invokes unlink on ID, leading to the complete deletion of the object and its associated link).
fields.command.unlink( ID ) – Sever the connection to the linked record identified by ID (terminate the association between the two entities without removing the target object itself).
fields.command.link( ID ) – Establish a link to an existing record identified by ID (creates an association).
fields.command.clear( ) – Dissociate from all (similar to employing (3, ID) for every linked record).
fields.command.set([ IDs ]) – Substitute the array of linked IDs (similar to employing (5) followed by (4, ID) for each ID in the list).