Are you looking to customize your Odoo 17 modules? Adding settings for Odoo 17 modules can significantly enhance their functionality. Consequently, this tutorial provides a comprehensive, code-focused guide to adding configurable settings to your custom Odoo modules. Moreover, we will explore the necessary files and code modifications.
Understanding Odoo 17 Module Settings
First, let’s clarify what module settings are. Module settings allow users to configure the behavior of a module without modifying the code directly. Therefore, this provides flexibility and allows for customization based on specific needs.
Why Add Settings to Your Odoo Modules?
Adding settings offers several benefits:
- Flexibility: Users can adjust module behavior without needing developer intervention.
- Customization: Settings allow tailoring the module to different business requirements.
- User Experience: Well-defined settings improve the user experience by providing clear configuration options.
- Maintainability: Settings centralize configuration, making it easier to manage and update.
Setting Up Your Odoo Module
Before adding settings, ensure you have a custom module. If not, create a basic module structure:
- Create a Module Folder: Create a new folder for your module (e.g.,
my_custom_module
). - Create
__manifest__.py
: This file describes your module.# -*- coding: utf-8 -*- { 'name': "My Custom Module", 'version': '1.0', 'summary': "Adds custom settings", 'description': """ This module adds custom settings to Odoo 17. """, 'author': "Your Name", 'category': 'Uncategorized', 'depends': ['base'], # Add dependencies here 'data': [ 'views/res_config_settings_views.xml', ], 'installable': True, 'application': True, 'auto_install': False, }
- Create
__init__.py
: Import necessary files.# -*- coding: utf-8 -*- from . import models
- Create a
models
folder, and inside create another__init__.py
file.# -*- coding: utf-8 -*- from . import res_config_settings
Creating the Settings Model
Next, create a Python file (res_config_settings.py
) within your models
directory to define the settings model.
# -*- coding: utf-8 -*-
from odoo import fields, models
class ResConfigSettings(models.TransientModel):
_inherit = 'res.config.settings'
my_custom_setting = fields.Char(string="My Custom Setting", config_parameter='my_custom_module.my_custom_setting')
_inherit = 'res.config.settings'
: This line inherits from Odoo’s base settings model.my_custom_setting
: This defines a new setting field.config_parameter
: This attribute links the field to a system parameter, allowing Odoo to store and retrieve its value.
Creating the Settings View
Now, create an XML file (res_config_settings_views.xml
) within a views
folder to define the settings view.
<odoo>
<data>
<record id="res_config_settings_view_form" model="ir.ui.view">
<field name="name">res.config.settings.view.form.inherit.my.custom.module</field>
<field name="model">res.config.settings</field>
<field name="priority">1</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="My Custom Module" string="My Custom Module" data-key="my_custom_module">
<h2>My Custom Settings</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">
<label for="my_custom_setting"/>
<div class="text-muted">
Enter a value for your custom setting
</div>
<div class="content-group">
<div class="mt16">
<field name="my_custom_setting"/>
</div>
</div>
</div>
</div>
</div>
</div>
</xpath>
</field>
</record>
<record id="my_custom_module_config_settings_action" model="ir.actions.act_window">
<field name="name">Settings</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' : 'my_custom_module'}</field>
</record>
<menuitem id="my_custom_module_menu_config" name="Settings" parent="base.menu_administration" sequence="0" action="my_custom_module_config_settings_action"/>
</data>
</odoo>
inherit_id
: This line specifies that we are inheriting from the base settings view.xpath
: This selects the location within the base view where our custom settings will be added.data-key
: Should be your module name.menuitem
: This adds a menu item under “Settings” to access your module’s configuration.
Adding More Settings
You can add more settings by adding more fields to your res_config_settings.py
and corresponding elements to your XML view. For example:
# In res_config_settings.py
my_boolean_setting = fields.Boolean(string="Enable Feature", config_parameter='my_custom_module.my_boolean_setting')
my_selection_setting = fields.Selection([('option1', 'Option 1'), ('option2', 'Option 2')], string="Select Option", config_parameter='my_custom_module.my_selection_setting')
<!-- In res_config_settings_views.xml -->
<div class="col-12 col-lg-6 o_setting_box">
<div class="o_setting_left_pane">
<field name="my_boolean_setting"/>
</div>
<div class="o_setting_right_pane">
<label for="my_boolean_setting"/>
<div class="text-muted">
Enable or disable a specific feature
</div>
</div>
</div>
<div class="col-12 col-lg-6 o_setting_box">
<div class="o_setting_right_pane">
<label for="my_selection_setting"/>
<div class="text-muted">
Choose from available options
</div>
<div class="content-group">
<div class="mt16">
<field name="my_selection_setting"/>
</div>
</div>
</div>
</div>
Using Settings in Your Module
To use the settings values in your module’s code, you can retrieve them using self.env['ir.config_parameter'].sudo().get_param()
.
# Example within a method in your module
setting_value = self.env['ir.config_parameter'].sudo().get_param('my_custom_module.my_custom_setting')
if setting_value:
# Do something based on the setting value
print(f"My custom setting value: {setting_value}")
boolean_setting = self.env['ir.config_parameter'].sudo().get_param('my_custom_module.my_boolean_setting')
if boolean_setting == 'True':
print("Boolean setting is enabled")
selection_setting = self.env['ir.config_parameter'].sudo().get_param('my_custom_module.my_selection_setting')
if selection_setting == 'option1':
print("Option 1 is selected")
Adding Settings to Existing Modules
If you want to add a setting to an existing Odoo module (e.g., Sales, Inventory), you can still inherit from res.config.settings
and add your fields. However, you’ll need to modify the xpath in your XML view to target the correct location within the existing module’s settings view. You can find the correct xpath by inspecting the existing view in Odoo’s developer mode.
Best Practices
- Use Descriptive Names: Choose clear and descriptive names for your settings fields and labels.
- Provide Help Text: Use the
text-muted
div in the XML to provide helpful descriptions for each setting. - Group Related Settings: Use
<h2>
and<div>
elements to group related settings logically. - Test Thoroughly: Test your settings thoroughly to ensure they work as expected.
- Consider Dependencies: If your custom setting depends on another module, add it to the
depends
list in your__manifest__.py
file. For example, if your custom setting extends the Sales module, you would add'depends': ['sale'],
.
Conclusion
Adding settings to your Odoo 17 modules provides a powerful way to make them configurable and adaptable to different user needs. By following the steps outlined in this tutorial, you can create flexible and user-friendly modules. Remember to use descriptive names, provide helpful descriptions, and test your settings thoroughly. By incorporating these techniques, you can significantly enhance the usability and maintainability of your Odoo modules. This approach allows for a more dynamic and adaptable system, catering to a wider range of business requirements.
Discover more from teguhteja.id
Subscribe to get the latest posts sent to your email.