res.config.settings¶
Based on https://github.com/odoo/odoo/blob/10.0/odoo/addons/base/res/res_config.py
res.config.settings is a base configuration wizard for application settings. It provides support for setting
default values, assigning groups to employee users, and installing modules.
To make such a ‘settings’ wizard, define a model like:
class MyConfigWizard(models.TransientModel):
_name = 'my.settings'
_inherit = 'res.config.settings'
default_foo = fields.type(..., default_model='my.model')
group_bar = fields.Boolean(..., group='base.group_user', implied_group='my.group')
module_baz = fields.Boolean(...)
other_field = fields.type(...)
The method execute (Apply button) provides some support based on a naming convention:
- For a field like
default_XXX,executesets the (global) default value of the fieldXXXin the model named bydefault_modelto the field’s value. - For a boolean field like
group_XXX,executeadds/removes ‘implied_group’ to/from the implied groups of ‘group’, depending on the field’s value. By default ‘group’ is the group Employee. Groups are given by their xml id. The attribute ‘group’ may contain several xml ids, separated by commas. - For a boolean field like
module_XXX,executetriggers the immediate installation of the module namedXXXif the field has valueTrue. - For the other fields, the method
executeinvokes all methods with a name that starts withset_; such methods can be defined to implement the effect of those fields.
The method default_get retrieves values that reflect the current status of the
fields like default_XXX, group_XXX and module_XXX. It also invokes all methods
with a name that starts with get_default_; such methods can be defined to provide
current values for other fields.
Example¶
from openerp import models, fields, api
PARAMS = [
("login", "apps_odoo_com.login"),
("password", "apps_odoo_com.password"),
]
class Settings(models.TransientModel):
_name = 'apps_odoo_com.settings'
_inherit = 'res.config.settings'
login = fields.Char("Login")
password = fields.Char("Password")
@api.multi
def set_params(self):
self.ensure_one()
for field_name, key_name in PARAMS:
value = getattr(self, field_name, '').strip()
self.env['ir.config_parameter'].set_param(key_name, value)
def get_default_params(self, cr, uid, fields, context=None):
res = {}
for field_name, key_name in PARAMS:
res[field_name] = self.env['ir.config_parameter'].get_param(key_name, '').strip()
return res
Update settings on module install¶
To update settings from any res.config.settings do as follows:
default_XXX¶
TODO
group_XXX¶
Add implied group(s) to a group via implied_ids field:
<record model="res.groups" id="base.group_user">
<field name="implied_ids" eval="[
(4, ref('my.group'))
]"/>
</record>
module_XXX¶
Add XXX to the “depends” parameter in the __manifest__.py file.
Other fields¶
Usually, other fields are saved to ir.config_parameter, so just update ir.config_parameter, for example:
<function model="ir.config_parameter" name="set_param" eval="(
'pos_debt_notebook.debt_type', 'credit'
)" />