How it works...

For an explanation of step 1 to step 3, refer to the Creating new records recipe in Chapter 6, Basic Server-side Development.

Step 4 calls the _onchange_spec method on the model, passing no argument. This method will retrieve the updates that are triggered by the modification of the other field. It does this by examining the form view of the model (remember that onchange methods are normally called by the web client).

Step 5 calls the onchange(values, field_name, field_onchange) method of the model, with three arguments:

  • values: The list of values we want to set on the record. You need to provide a value for all the fields you expect to be modified by the onchange method. In the recipe, we set book_ids to False for this reason.
  • field_name: A list of fields for which we want to trigger the onchange methods. You can pass an empty list, and ORM will use the fields defined in values. However, you will often want to specify this list manually to control the order of evaluation, in case different fields can update a common field.
  • field_onchange: The onchange specifications that were computed in step 4. This method finds out which onchange methods must be called, and in what order, and it returns a dictionary, which can contain the following keys:
    • value: This is a dictionary of newly-computed field values. This dictionary only features keys that are in the values parameter passed to onchange(). Note that the Many2one fields are mapped to a tuple that contains (id, display_name) as an optimization for the web client.
    • warning: This is a dictionary that contains a warning message that the web client will display to the user.
    • domain: This is a dictionary that maps field names to new validity domains.

Generally, when manually playing with onchange methods, we only care about what is in value.

Step 6 updates our initial values dictionary with the values computed by onchange. We process the values that correspond to the Many2one fields to only keep the id. To do this, we take advantage of the fact that these fields are only those whose values are returned as a tuple.

Finally Step 7 creates the record.

..................Content has been hidden....................

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