Odoo, an influential open-source ERP and business management software, empowers users with the flexibility to tailor and enhance its functionality using custom modules. In custom module development, a crucial aspect involves incorporating a settings menu, granting users the capability to fine-tune multiple options based on their unique needs.
This blog post guides you through a step-by-step process, illustrating how to seamlessly integrate a settings menu for custom modules in Odoo 17. This empowers users to effortlessly personalize their module settings.
To initiate the process, the first step is creating a custom module. This involves defining a configuration model responsible for housing the settings specific to our module. This model inherits from the ‘res.config.settings’ class, a foundational component provided by Odoo. Open the primary Python file of your module and incorporate the following code.
class ResConfigSettings(models.TransientModel):
_inherit = 'res.config.settings'
contract_type = fields.Selection(
[('monthly', 'Monthly'), ('half_yearly', '6 Months'),
('yearly', 'Yearly')],
string="Contract Type",
config_parameter='employee_contract.contract_type',
help="Select contract types from the selection field")
The provided code introduces a new field to the ‘res.config.settings’ model, enabling the selection of an employee contract type. This field utilizes the ‘config_parameter’ property, which leverages a global model accessible throughout the Odoo system. This feature facilitates the storage and retrieval of configuration parameters within the database. To ensure the selected data is appropriately saved, the ‘config_parameter’ property is indispensable in field declarations. Upon saving the record, clarity emerges regarding the presence or addition of data to the field. In this context, the ‘config_parameter’ for the ’employee_contract.contract_type’ field is established, where ’employee_contract’ represents the module name, and ‘contract_type’ signifies the field name in the declaration.
After inheriting from ‘res.config.settings,’ the next step involves creating a view for our new module. To achieve this, include the following XML code.
<record id="res_config_settings_view_form" model="ir.ui.view">
<field name="name">
res.config.settings.view.form.inherit.employee.contract
</field>
<field name="model">res.config.settings</field>
<field name="priority" eval="15"/>
<field name="inherit_id" ref="base.res_config_settings_view_form"/>
<field name="arch" type="xml">
<xpath expr="//form" position="inside">
<app data-string="Employee Management" string="Employee Management" name="employee_contract">
<block title="Employee Management Settings" name="employee_management">
<setting string="Contract Type"
help="Select the Contract Type"
id="contract_type_setting">
<field name="contract_type"/>
<div class="content-group"
invisible="not contract_type"
id="group_contract_type_setting">
<div class="text-warning mt16">
<strong>Save</strong>
this page and come back here to set up the
feature.
</div>
</div>
</setting>
</block>
</app>
</xpath>
</field>
</record>
The provided XML code defines a view form in the Odoo framework for the “res.config.settings” model, extending the base view with the ID “base.res_config_settings_view_form.” The customized view introduces an “Employee Management” section, encapsulated within an app and block structure. Within this section, a setting for “Contract Type” is defined, including a field to select the contract type. The XML code also incorporates a warning message, displayed conditionally based on the presence of a contract type, advising users to save the page and return for further feature setup.
Subsequently, incorporate the ‘ir.actions.act.window’ model, which encapsulates a window action, outlining the behavior of the associated menu item.
<record id="res_config_settings_action" model="ir.actions.act_window">
<field name="name">Configuration</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">res.config.settings</field>
<field name="view_mode">form</field>
<field name="target">inline</field>
<field name="context">{'module' : 'employee_contract'}</field>
</record>
<menuitem id="employee_management_menu_root"
name="Employee Management"
sequence="1"/>
<menuitem id="employee_management_menu_action"
name="Configuration"
parent="employee_contract.employee_management_menu_root"
sequence="6"/>
<menuitem id="employee_contract_settings_menu_action"
name="Settings"
parent="employee_contract.employee_management_menu_action"
action="res_config_settings_action"
sequence="7"/>
The above code establishes actions and menu items in Odoo for the ‘res.config.settings’ model. Specifically, the ‘res_config_settings_action’ record defines a window action named “Configuration” with attributes such as the associated model, view mode set to ‘form,’ and a specified context. This action encapsulates the behavior of the menu item related to configuring settings for Employee Management.
The subsequent menu items contribute to the hierarchical structure of the application’s menu. “Employee Management” serves as the root menu item, and under this, the “Configuration” menu item is nested. Further, the “Settings” menu item, associated with the ‘res_config_settings_action’ action, is situated under “Configuration.”
Following these steps, you can execute the code and observe the field values presented as a selection field with predefined options on the user interface screen.
Configuring the provided code involves a straightforward process within an Odoo module, requiring the definition of appropriate XML and Python code. By following these steps, you can significantly augment the functionality and user experience of your Odoo module, offering users a dedicated settings menu to configure module-specific options. This customization not only tailors the module to unique requirements but also contributes to an improved and more intuitive user interface.
To read more about adding a settings menu for custom modules in Odoo 16, refer to our blog How to Add a Settings Menu for Custom Modules in Odoo 16