Skip to content
Home » My Blog Tutorial » Mastering Odoo Settings: Configure Your Custom Modules Effortlessly

Mastering Odoo Settings: Configure Your Custom Modules Effortlessly

Odoo settings configuration

Odoo Settings Configuration. Are you looking to enhance your Odoo custom modules with powerful configuration options? In this guide, we’ll walk you through the process of adding settings pages, configuring default values, and managing module installation through settings. By the end, you’ll have a firm grasp on Odoo settings configuration, allowing you to create more flexible and user-friendly modules.

Why Odoo Settings Matter

Odoo Settings Configuration. First and foremost, well-designed settings pages give your users control over module behavior. For instance, you can let them set default values or toggle features on and off. This flexibility makes your modules more adaptable to various business needs.

Adding a Settings Page to Your Custom Module

Let’s dive into the steps to create a settings page:

  1. Create a new Python file named res_config_settings.py in your module’s models folder.
  2. In this file, inherit the res.config.settings model:
from odoo import models, fields

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

    use_number_of_seats = fields.Boolean(string='Number of Seats', config_parameter='openacademy.use_number_of_seats')
    session_number_of_seats = fields.Integer(string='Number of Seats', related='company_id.session_number_of_seats', readonly=False)
  1. Next, create an XML file for the settings view, typically named res_config_settings_views.xml:
<odoo>
    <record id="res_config_settings_view_form" model="ir.ui.view">
        <field name="name">res.config.settings.view.form.inherit.openacademy</field>
        <field name="model">res.config.settings</field>
        <field name="priority" eval="70"/>
        <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" string="Open Academy" data-key="openacademy">
                    <h2>Session 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">
                                <field name="use_number_of_seats"/>
                            </div>
                            <div class="o_setting_right_pane">
                                <label for="use_number_of_seats"/>
                                <div class="text-muted">
                                    Set a default number of seats for sessions
                                </div>
                                <div class="content-group" attrs="{'invisible': [('use_number_of_seats','=',False)]}">
                                    <div class="mt16">
                                        <field name="session_number_of_seats" class="o_light_label" widget="integer"/>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </xpath>
        </field>
    </record>
</odoo>
  1. Finally, add these files to your module’s __manifest__.py:
{
    # ... other manifest data ...
    'data': [
        # ... other data files ...
        'views/res_config_settings_views.xml',
    ],
}

Configuring Default Values

To set default values that users can modify, we use the config_parameter attribute in our fields. This approach stores the value globally, making it accessible throughout the system.

For company-specific settings, we use the related attribute to link the field to a company model. This way, each company can have its own configuration.

Installing and Uninstalling Modules Through Settings

To allow users to install or uninstall modules directly from the settings page, add a Boolean field for each optional module:

module_openacademy_website = fields.Boolean(string="Website Integration")

Then, in your XML view, add a checkbox for this field:

<div class="col-12 col-lg-6 o_setting_box">
    <div class="o_setting_left_pane">
        <field name="module_openacademy_website"/>
    </div>
    <div class="o_setting_right_pane">
        <label for="module_openacademy_website"/>
        <div class="text-muted">
            Enable website features for Open Academy
        </div>
    </div>
</div>

When users check or uncheck this box and save the settings, Odoo automatically installs or uninstalls the corresponding module.

Conclusion

Odoo Settings Configuration. By following these steps, you can create powerful and flexible settings pages for your Odoo custom modules. This approach gives your users more control over module behavior, leading to a better user experience. Remember to use clear labels and helpful descriptions in your settings to guide users effectively.

As you continue to develop your Odoo skills, keep exploring new ways to enhance your modules. Happy coding!

For more information on Odoo development, check out the official Odoo documentation.


Discover more from teguhteja.id

Subscribe to get the latest posts sent to your email.

Tags:

Leave a Reply

Optimized by Optimole
WP Twitter Auto Publish Powered By : XYZScripts.com

Discover more from teguhteja.id

Subscribe now to keep reading and get access to the full archive.

Continue reading