Using the context to compute default values

The wizard we are presenting requires the user to fill in the name of the member in the form. There is a feature of the web client that we can use to save some typing. When an action is executed, the context is updated with some values that can be used by wizards:

Key

Value

active_model

This is the name of the model related to the action. This is generally the model being displayed onscreen.

active_id

This indicates that a single record is active and provides the ID of that record.

active_ids

If several records are selected, this will be a list with the IDs. This happens when several items are selected in a tree view when the action is triggered. In a form view, you get [active_id].

active_domain

This is an additional domain on which the wizard will operate.

These values can be used to compute the default values of the model, or even directly in the method called by the button. To improve on the example in this recipe, if we had a button displayed on the form view of a res.partner model to launch the wizard, the context of the creation of the wizard would contain {'active_model': 'res.partner', 'active_id': <partner id>}. In that case, you could define the member_id field to get a default value computed by the following method:

def _default_member(self): 
    if self.context.get('active_model') == 'res.partner': 
        return self.context.get('active_id', False)
..................Content has been hidden....................

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