There's more...

There are three options available if you want to write new values to the fields of records:

  • Option one is the one that was explained in this recipe. It works in all contexts by assigning values directly to the attribute representing the field of the record. It isn't possible to assign a value to all recordset elements in one go, so you need to iterate on the recordset, unless you are certain that you are only handling a single record.
  • Option two is to use the update() method by passing dictionary mapping field names to the values you want to set. This also only works for recordsets of a length of 1. It can save some typing when you need to update the values of several fields at once on the same record. Here's step two of the recipe, rewritten to use this option:
@api.multi
def change_update_date(self):
self.ensure_one()
self.update({
'date_updated': fields.Datetime.now(),
'another_field': 'value'
...
})
  • Option three is to call the write() method, passing a dictionary that maps the field names to the values you want to set. This method works for recordsets of arbitrary size and will update all records with the specified values in one single database operation when the two previous options perform one database call per record and per field. However, it has some limitations:
    • It does not work if the records are not yet present in the database. (Refer to the Writing onchange methods recipe in Chapter 9, Advanced Server-Side Development Techniques, for more information on this.)
    • It requires a special format when writing relational fields, similar to the one used by the create() method,. Check the following table for the format that's used to generate different values for the relational fields:

Tuple

Effect

(0, 0, dict_val)

This creates a new record that will be related to the main record.

(1, id, dict_val)

This updates the related record with the specified ID with the supplied values.

(2, id)

This removes the record with the specified ID from the related records and deletes it from the database.

(3, id)

This removes the record with the specified ID from the related records. The record is not deleted from the database.

(4, id)

This adds an existing record with the supplied ID to the list of related records.

(5, )

This removes all the related records, equivalent to calling (3, id) for each related id.

(6, 0, id_list)

This creates a relation between the record being updated and the existing record, whose IDs are in the Python list called id_list.

At the time of writing, the official documentation is outdated and mentions that the operation numbers 3, 4, 5, and 6 are not available on the One2many fields, which is no longer true. However, some of these may not work with One2many fields, depending on constraints on the models. For instance, if the reverse Many2one relation is required, then operation 3 will fail because it will result in an unset Many2one relation.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset