Enable Dark Mode!
how-to-add-a-settings-menu-for-custom-modules-in-odoo-18.jpg
By: Unnimaya CO

How to Add a Settings Menu for Custom Modules in Odoo 18

Technical

Odoo is a highly adaptable open-source platform that serves as a comprehensive solution for managing business processes. Its modular design enables developers to build custom applications that address unique operational challenges. One important aspect of customizing Odoo is the inclusion of a configuration interface, where users can personalize settings and fine-tune features to align with their workflow. This flexibility ensures that businesses can optimize performance and streamline operations without compromising their specific requirements.

This guide provides a detailed walkthrough on integrating a settings menu into custom modules in Odoo 18, enabling users to easily configure and customize their module preferences.

The process begins with the creation of a custom module. At the core of this setup is the definition of a configuration model, which is designed to store and manage the module’s settings. This model leverages Odoo’s built-in 'res.config.settings' class, offering a structured and efficient way to handle configurations.

To get started, open the primary Python file of your custom module and implement the following code snippet.

from odoo import fields,models

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")

This code snippet facilitates the addition of a field to the 'res.config.settings' model, enabling users to choose an employee contract type. By utilizing the 'config_parameter' property, this field becomes part of a global configuration system in Odoo. The 'config_parameter' ensures that the configuration data is consistently stored and retrievable across the entire Odoo environment.

Once the user saves the settings, the value in the field is stored in the database, making it easy to track changes. In this case, the 'config_parameter' is defined as 'employee_contract.contract_type', where 'employee_contract' refers to the module, and 'contract_type' refers to the specific setting. This method offers a clean and efficient way to store custom configuration settings, ensuring they are readily available whenever needed.

Once you have inherited from the 'res.config.settings' model, the next step is to create a view for your custom module. This view will define how the settings interface appears to the user. You can achieve this by adding the following XML code to your module. This will ensure that the new configuration field is displayed properly in the Odoo interface, allowing users to easily interact with the settings of your module.

Here’s the XML code to create the view:

<?xml version="1.0" encoding = "utf-8"?>
<odoo>
   <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>
</odoo>

The XML code provided outlines a custom form view for the 'res.config.settings' model in Odoo, extending the default form view identified by "base.res_config_settings_view_form." This extension introduces a new section titled "Employee Management," which is structured within an app and block format to ensure proper organization in the settings interface.

Within the "Employee Management" section, a new setting called "Contract Type" is included, featuring a dropdown field that allows users to select the desired contract type. Additionally, the XML code includes a conditional warning message. This message prompts users to save their changes and return to complete the setup of additional features, but only if a contract type is selected. This approach enhances user guidance and ensures that essential steps in the setup process are not overlooked.

Next, to ensure that the new settings view is accessible from the Odoo interface, you need to integrate the 'ir.actions.act.window' model. This model defines a window action that links the view to a specific menu item. By doing so, users can access the settings menu from the main Odoo dashboard or the corresponding application.

Here’s how to include the 'ir.actions.act.window' model in your module, which will define the action behavior for the newly created menu item:

<?xml version="1.0" encoding = "utf-8"?>
<odoo>
<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"/>
</odoo>

The XML code for the menu items defines a hierarchical structure within the Odoo application, making it easy for users to navigate through the settings. "Employee Management" serves as the root menu item, and under this main menu, the "Configuration" menu item is nested. Within the "Configuration" section, the "Settings" menu item is placed, linking it to the 'res_config_settings_action' action that opens the settings form for configuring employee contract types.

Once you execute the code, you’ll notice that the field for selecting the contract type appears as a dropdown selection field in the user interface. This field will display predefined options that users can choose from, making it easy to configure employee contract settings directly within the Odoo system. By following these steps, you can provide a streamlined and user-friendly experience for managing the configurations.

How to Add a Settings Menu for Custom Modules in Odoo 18-cybrosys

Configuring the provided code is a simple yet effective process within an Odoo module, which involves defining the necessary XML and Python code to create a seamless user experience. By following the steps outlined, you can enhance the functionality of your Odoo module by providing a dedicated settings menu that allows users to configure module-specific options with ease. This customization offers users the flexibility to adjust settings based on their unique requirements, while also contributing to a more intuitive and user-friendly interface.

Integrating a settings menu into your Odoo module not only streamlines the configuration process but also empowers users to personalize their experience, making the module more adaptable to their business needs. By leveraging Odoo's flexible architecture, you can extend the functionality of your modules, resulting in a more efficient and customized solution that aligns with specific operational goals. This approach ultimately improves the overall user experience, ensuring that Odoo becomes an even more valuable tool for businesses.

To read more about How to Add a Settings Menu for Custom Modules in Odoo 17, refer to our blog How to Add a Settings Menu for Custom Modules in Odoo 17.


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



whatsapp_icon
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message