Creating a Settings Menu in Odoo Custom module
Creating a Custom Module Settings Menu in Odoo 16
Odoo is a robust open-source ERP and business management software that offers the flexibility to enhance and tailor its capabilities through custom modules. An important facet of custom module development involves incorporating a settings menu, which empowers users to fine-tune various options to meet their precise needs. In this informative blog post, we will guide you through the systematic procedure of integrating a settings menu for custom modules in Odoo 16. This enables users to effortlessly customize their module settings and adapt them to their preferences.
To begin, the initial step is creating a custom module. Within this Odoo module, we must establish a configuration model dedicated to storing the settings specific to our custom module. This model will inherit from Odoo’s res. config.settings class. To implement this, open the primary Python file of your module and insert 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 snippet facilitates the selection of an employee contract type by introducing a new field within the res. config.settings model. This field leverages the config_parameter property, which is a global model accessible throughout the Odoo system. This property enables the storage and retrieval of configuration parameters within the database. Utilizing the config_parameter property in the field definition is essential to successfully store the chosen data. Once the record is saved, it becomes evident whether a selection has been made or data has been added to the field. In this case, with “employee_contract” as the module name and “contract_type” as the field name, the config_parameter is represented as “employee_contract.contract_type.”
Once you’ve inherited the res.config.settings, the next step is to create a view for your new module. To accomplish 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="//div[hasclass('settings')]" position="inside"> <div class="app_settings_block" data-string="Employee Management" string="Employee Management" data-key="employee_contract"> <h2>Employee Management</h2> <div class="row mt16 o_settings_container"> <div class="col-12 col-lg-6 o_setting_box"> <div class="o_setting_left_pane"/> <div class="o_setting_right_pane"> <span class="o_form_label">Contract Type</span> <div class="text-muted content-group mt16"> <field name="contract_type"/> </div> </div> </div> </div> </div> </xpath> </field> </record>Request Your Free Quote
The provided XML code serves to establish a fresh session within the settings and incorporates the new field into the configuration settings. Additionally, it introduces a new <div> element into the base view, specifically designed to accommodate “Employee Management” settings. This section is outfitted with a header and is distinguished by the “data-key” attribute set to “employee_contract.” Within this section, there’s a <span> element featuring the “o_form_label” class, which prominently displays the label “Contract Type.”
Within this <div> element, there’s a form field that showcases the content of the “contract_type” field. The field tag plays a pivotal role in presenting the field value to users or enabling them to input data within the form.
Following this, include the “ir.actions.act.window” model, which serves as a representation of a window action. And delineates the behavior associated with the 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>
The context field serves the purpose of transmitting supplementary contextual information to the action window identified as “res_config_settings_action.” This contextual data is defined as a dictionary containing a solitary key-value pair. A context variable (key) is designated as a ‘module’, and the value associated with this variable is ’employee_contract’. This assignment effectively signifies that the context variable ‘module’ is set to the value ’employee_contract’.
Menu items serve as a navigational gateway, enabling users to access various views and actions within a custom module.
<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 provided code accomplishes the addition of a configuration menu, along with a submenu dedicated to settings within the module. Furthermore, the window action has been seamlessly integrated into this submenu. With these configurations in place, executing the code will result in the display of field values as a selection field with predefined values on the user interface screen.
Configuring the provided code is a straightforward procedure for introducing a new settings menu into an Odoo module. This entails defining the necessary XML and Python code within your module. These steps will enhance the functionality of your Odoo module. Granting users access to a dedicated settings menu where they can customize module-specific options.
DOWNLOAD ERP
"Unlock the Full Potential of Your Business with Odoo ERP!"
"Get a Cost Estimate for Your ERP Project, Absolutely FREE!"
Get a Free Quote