Learn to add custom settings to Odoo modules with practical examples. Create configuration options that control validation rules, notifications, and business logic in Odoo 17 development.
Introduction
As an Odoo developer, you've probably faced this scenario: You write a validation rule or business logic, but later realize it needs to be configurable for different clients or situations. In Odoo development, adding a dedicated configuration page to your module is a professional practice that makes your module more flexible and user-friendly. In this comprehensive guide, you'll learn how to create custom settings for your Odoo module—from creating the model and views to implementing and accessing these configurations in your code.
Why Custom Settings are Crucial for Development
Before we dive into the code, let's understand why custom settings are crucial:
- User Control: Allow administrators to tweak module behavior
- Professionalism: A settings page makes your module look polished
- Flexibility: Easy adjustments without code changes
- Multi-company Support: Different settings for different companies
- Security: Controlled access to configuration
Step 1: Creating the Settings Model
Let's start by creating a model that will store our configuration values. We'll create a simple case for "account.move" model with customizable settings.
For example I override function to bock double reverse journal
from odoo import api, models, fields, _from
odoo.exceptions import ValidationError
class AccountMoveReverse(models.TransientModel):
_inherit = 'account.move.reversal'
def refund_moves(self):
for move in self.move_ids:
exist = self.env['account.move'].search([('reversed_entry_id','=',move.id),('state','!=','cancel')],limit=1)
print(exist)
if exist :
raise ValidationError("This Journal %s has been reverse %s" % (move.name, exist.name))
return super().refund_moves()
Next thing, we going to set up or create configurable setting for our customize function
from odoo import fields, models
class SgeedeResConfig(models.TransientModel):
_inherit = 'res.config.settings'
block_double_reverse = fields.Boolean( string="Block Double Reversal", config_parameter="base.block_double_reverse", help="When enabled, prevents reversing an already reversed journal entry. "
"Disable if your business processes require double reversals.")
Key improvements made:
- ✅ Added config_parameter: This automatically handles storage in ir.config_parameter
- ✅ Added default=True: Most companies want this protection
- ✅ Added help text: Explains when to enable/disable
Step 2: Update Your Validation Logic
add this line to the function:
self.env['ir.config_parameter'].sudo().get_param('base.block_double_reverse')
like this:
def refund_moves(self):
for move in self.move_ids:
exist = self.env['account.move'].search([('reversed_entry_id','=',move.id),('state','!=','cancel')],limit=1)
print(exist)
if exist and self.env['ir.config_parameter'].sudo().get_param('base.block_double_reverse'):
raise ValidationError("This Journal %s has been reverse %s" % (move.name, exist.name))
return super().refund_moves()
Step 3: Create the Settings Interface
Let's add this setting to Odoo's configuration:
<?xml version="1.0" encoding="utf-8"?>
<odoo noupdate="1">
<record id="sgeede_res_config_sett_view_form" model="ir.ui.view">
<field name="name">sgeede_res.config.sett.view.form.inherit.base.setup</field>
<field name="model">res.config.settings</field>
<field name="priority" eval="0"/>
<field name="inherit_id" ref="base.res_config_settings_view_form"/>
<field name="arch" type="xml">
<xpath expr="//block[@name='performance']" position="after">
<block title="SGEEDE" name="sgeede">
<setting id="block_double_reverse">
<field name="block_double_reverse"/>
</setting>
</block>
</xpath>
</field>
</record>
<record id="param_block_double_reverse" model="ir.config_parameter">
<field name="key">base.block_double_reverse</field>
<field name="value">True</field>
</record>
</odoo>
Conclusion
This code was 90% there—adding the config_parameter attribute was the key insight. The rest is about making it robust, user-friendly, and well-documented.
Remember: The best Odoo modules don't just work—they adapt to the business. Your configurable validation rule does exactly that!
