Enable Dark Mode!
how-to-set-up-use-system-parameters-in-the-odoo-15.jpg
By: Dilshad A

How to Set Up & Use System Parameters in the Odoo 15

Technical Odoo 15

In Odoo, a system parameter is a model with a key-value pair structure that is used to hold the application configuration. System parameters are simply used to store settings; each model must explicitly programme the actual implementation as needed.

An ir.config parameter is the model for the system parameter. System parameters are typically used to enable or disable a specific feature, save the default values, etc. Since the system parameter is solely intended to store configuration, it has no restrictions on what it may be used for in our Odoo application.

How to create a system parameter?

A system parameter can be created in several ways. We can first create it via the user interface. Enter debug mode while logged in as an administrator, then navigate to the Settings >> Technical >> Parameters >> System Parameters menu. After that, press the Create button.

how-to-set-up-use-system-parameters-in-the-odoo-15-cybrosys

Any text can be entered in the key field as long as it is specific to the database. Avoid using spaces or other special characters, and always attempt to use lowercase letters.

Since the data type for the value field is a string, any value that can be processed in Python may be entered here. If we wish to process this field further with Python, my recommendation is to populate this field with text in integer format, comma-separated values, or JSON without spaces.

The second method is to generate a system parameter directly from the module, eliminating the requirement for the user to explicitly create it when the module is installed. Here's an illustration of how to accomplish it.

<record id="allowed_warehouse_parameter" model="ir.config_parameter">
    <field name="key">allowed_warehouse</field>
    <field name="value">1,2,3,4,5</field>
</record>

To make it simpler for users to access, we may also add the system parameter to the Configuration or Settings form.

In order to accomplish this, we must first develop a model that inherits from the res.config.settings model. Create a field with the data type Char and then enter the key name of the desired system parameter in the config parameter as displayed below. 

class ResConfigSettings(models.TransientModel):
    _inherit = 'res.config.settings'
Allowed_warehouse = fields.Char(string = 'Allowed Warehouse',
    config_parameter = 'allowed_warehouse')

Then, by inheriting its view, add those fields to the Configuration form; in this case, I'll do it using the Sales Configuration form's allowed warehouse field.

<record id="res_config_settings_view_form" model="ir.ui.view">
    <field name="name">res.config.settings.view.form.inherit</field>
    <field name="model">res.config.settings</field>
    <field name="priority" eval="5" />
    <field name="inherit_id" ref="sale.res_config_settings_view_form" />
    <field name="arch" type="xml">
        <xpath expr="//div[@id='sale_ebay']" position="after">
            <h2 class="mt32">New Configuration</h2>
            <div class="row mt16 o_settings_container">
                <div class="col-12 col-lg-6 o_setting_box">
                    <div class="o_setting_right_pane">
                        <label for="allowed_warehouse" />
                        <div class="text-muted">
                            <field name="allowed_warehouse" />
                        </div>
                    </div>
                </div>
            </div>
        </xpath>
    </field>
</record>

We can fill up the allowed warehouse system parameter's value as shown in the figure below by going to the Sales >> Configuration >> Settings menu. The information entered will be saved in the system parameter or model named ir.config parameter if we click the Save button.

how-to-set-up-use-system-parameters-in-the-odoo-15-cybrosys

System Parameter Read

We only need to invoke the get param method of the ir.config parameter model to obtain the system parameter. The first argument is the key of the system parameter we want to access, and the second argument is optional and is the default value that will be provided if the database does not contain the key for the system parameter we are trying to access. By altering the create and write functions, you can use this example to show how you leverage the system parameter to restrict warehouse access in sales orders.

from odoo import api, fields, models, exceptions
class SaleOrder(models.Model):
_inherit = 'sale.order'
@api.model
def create(self, vals):
# read the system parameter with key of 'allowed_warehouse'
# if not exist, it will return an empty string
# then split based on the comma character to convert it into a list
allowed_warehouse = self.env['ir.config_parameter'].get_param('allowed_warehouse', '').split(',')
# because the list value is stored in string, convert it to an integer
allowed_warehouse = list(map(lambda x: int(x), allowed_warehouse))
# check the warehouse field that selected by the user, is it in the list of warehouses that are allowed or not
if vals.get('warehouse_id', False) not in allowed_warehouse:
raise exceptions.ValidationError('You are not allowed to create the sales order in this warehouse !')

return super(SaleOrder, self).create(vals)

def write(self, vals):
# read the system parameter with key of 'allowed_warehouse'
# if not exist, it will return an empty string
# then split based on the comma character to convert it into a list
allowed_warehouse = self.env['ir.config_parameter'].get_param('allowed_warehouse', '').split(',')
# because the list value is stored in string, convert it to an integer
allowed_warehouse = list(map(lambda x: int(x), allowed_warehouse))
# check the warehouse field that selected by the user, is it in the list of warehouses that are allowed or not
if vals.get('warehouse_id', False) not in allowed_warehouse:
raise exceptions.ValidationError('You are not allowed to create the sales order in this warehouse !')
return super(SaleOrder, self).write(vals)

According to the code above, an error will appear, as seen in the image below if the user selects a warehouse that does not exist in the allowed warehouse system parameter.

Change the System Parameter's Value

We can use the set param method to modify the value of a system parameter. The system parameter's key, which we want to modify, is the first argument. Value serves as the second argument. This is an illustration of how to modify the value of a system parameter to keep track of the quantity of deleted Sales Orders.

def unlink(self):
for rec in self:
deleted = int(self.env['ir.config_parameter'].get_param('deleted_sale_order', '0')) + 1
self.env['ir.config_parameter'].set_param('deleted_sale_order', deleted)
return super(SaleOrder, self).unlink()

I believe it is preferable if you are familiar with some of the standard Odoo system parameters. Web.base.url is the first one. Fill in your server address in this system parameter, for instance, https://ngasturi.id:8069. When we create a URL to our server, we start with this system parameter. For instance, if we use the following code to build an email template,

<p>
    Follow <a href="${'/my/order'}"> this link </a> to see your order detail
</p>

The ‘href’ in the aforementioned code will automatically transform to https://ngasturi.id:8069/my/order when a consumer receives an email. So that the email template code won't need to be changed when the website's domain is changed.

Additionally, the bootstrap and CSS files used in the report are loaded from this system parameter's URL. Therefore, if your pdf report appears to be broken, there may be a problem with this system parameter.

There are instances where the value of the system parameter web.base.url will change automatically without our knowledge. It's a good idea to include the web.base.url.freeze system parameter with the value of True in order to stop this.


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



0
Comments



Leave a comment

 
whatsapp
location

Calicut

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

location

London

Cybrosys Limited
Alpha House,
100 Borough High Street, London,
SE1 1LB, United Kingdom

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