This question has been flagged
3 Replies
15552 Views

Hi!

How can I create a custom config. page for a module? A section where the user would fill some data, and that data would be retrieved in a module for its use

Thanks

Avatar
Discard

can you describe this in brief with some example ?

Best Answer

This changed very much in Odoo 11. To get an idea how to create Configuration pages in Odoo 11, my tip is to look for files named res_config_settings.py and res_config_settings.xml.

Here is a short example of a configuration in Odoo 11:

Example model

class Session(models.Model):
_name = 'openacademy.session'

name = fields.Char(required=True)
seats = fields.Integer(string="Number of seats")

The field 'seats' will get a default value configurable in the configuration that we will create. In odoo 11 all configuration is done in the same model, which we will extend via _inherit.

Configuration model res_config_settings.py

class ResConfigSettings(models.TransientModel):
_inherit = 'res.config.settings'

default_seats = fields.Integer(default_model='openacademy.session')
my_setting = fields.Char(string='My Setting')

def get_values(self):
res = super(ResConfigSettings, self).get_values()
res.update(
my_setting=self.env['ir.config_parameter'].sudo().get_param('openacademy.my_setting')
)
return res

def set_values(self):
super(ResConfigSettings, self).set_values()
self.env['ir.config_parameter'].sudo().set_param('openacademy.my_setting', self.my_setting)
Fields with the prefix 'default_' will be handled automatically by Odoo
to provide default values for fields in some models.
Other configuration values need to be handled manually in get_values and set_values.

Finally there is the view for this model. It is quite a lot code,
to match the same style of the other Odoo config pages.
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<record id="openacademy_settings_view2" model="ir.ui.view">
<field name="name">Openacademy Configuration</field>
<field name="model">res.config.settings</field>
<field name="inherit_id" ref="base.res_config_settings_view_form" />
<field name="arch" type="xml">
<xpath expr="//div[hasclass('settings')]" position="inside">
<div class="app_settings_block" data-string="Open Academy Data-String" string="Open Academy" data-key="openacademy">
<h2>General Settings</h2>
<div class="row mt16 o_settings_container">
<div class="col-xs-12 col-md-6 o_setting_box">
<div class="o_setting_right_pane">
<label for="my_setting"/>
<div class="text-muted">
Description text 1
</div>
<div class="content-group">
<div class="mt16">
<field name="my_setting" class="o_light_label"/>
</div>
</div>
</div>
</div>
<div class="col-xs-12 col-md-6 o_setting_box">
<div class="o_setting_right_pane">
<label for="default_seats"/>
<div class="text-muted">
Description text 2
</div>
<div class="content-group">
<div class="mt16">
<field name="default_seats" class="o_light_label"/>
</div>
</div>
</div>
</div>
</div>
</div>
</xpath>
</field>
</record>

<record id="openacademy_settings_action" model="ir.actions.act_window">
<field name="name">Openacademy Configuration</field>
<field name="res_model">res.config.settings</field>
<field name="view_id" ref="openacademy_settings_view2"/>
<field name="view_mode">form</field>
<field name="target">inline</field>
<field name="context">{'module' : 'openacademy'}</field>
</record>

<menuitem id="openacademy_settings_menu" name="Configuration"
parent="main_openacademy_menu" action="openacademy_settings_action"/>
</odoo>

Avatar
Discard
Best Answer

Hello,

You can check this question

also you can check the HR configuration in addons/hr check the res_config.py and res_config_view.xml it simple and straight forward one. also you can check the base res_config in /addons/base/res res_config.py check the res_config_settings class documentation

Regards

Avatar
Discard