There's more...

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

  • Option 1 is the one explained in the recipe, and it works in all contexts, assigning values directly on the attribute representing the field of the record. It is not 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 2 is to use the update() method by passing a dictionary mapping field names to the values you want to set. This also only works for recordsets of length 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 2 of the recipe, rewritten to use this option:
    @api.model 
    def add_contacts(self, partner, contacts): 
        partner.ensure_one() 
        if contacts: 
            today = fields.Date.context_today(self) 
            partner.update( 
                {'date': today, 
                 'child_ids': partner.child_ids | contacts} 
            ) 
  • Option 3 is to call the write() method, passing a dictionary mapping 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 using a special format when writing relational fields, similar to the one used by the create() method:

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 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 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
18.227.111.33