Enable Dark Mode!
how-to-add-custom-fields-to-existing-configuration-settings-in-odoo-16.jpg
By: Sumith Sivan

How to Add Custom Fields to Existing Configuration Settings in Odoo 16?

Technical Odoo 16

Users can customize the setting options in Odoo's Settings menu as they see fit. Each application in Odoo has choices for Configuration Settings, which makes it more convenient for users by enabling them to modify parameters in accordance with their business requirements. At times, it is also possible to use only some features. In this blog, let's look at adding new settings options and processing operations based on them.

Create a custom module discount_limit  for creating a limit for customers in 

discount. The discount limit of a customer rental can be managed using the model discount limit.

Here we will add a new field/custom field in the ‘res.config.settings’ model.

How to Add Custom Fields to Existing Configuration Settings in Odoo 16?-cybrosys

The option to set the maximum discount limit to the client in the order line can be enabled or disabled by adding a boolean field under settings. If the settings option is turned on, a field where the user can specify the maximum discount an individual customer may receive in a sale order will be visible.

There is a rental order made. You must inherit the model res.config.settings in order to create a new custom settings option under Res Configuration Settings. Add a boolean field called Discount Limit next.      

class ConfSetting(models.TransientModel):
   _inherit = "res.config.settings"
   discount_limit = fields.Boolean(string="Discount Limit", store=True)

Models of the Transient model type include the Res Config Settings model. The only difference between a transient model and a regular model is that a transient model's data is regularly removed. Therefore, it is not practical to store data using a transitory model.

Let's now examine how to add our custom option and inherit settings from the view.

<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
   <record id="res_config_settings_inheit_view" model="ir.ui.view">
       <field name="name">res.config.settings.inherit.view</field>
       <field name="model">res.config.settings</field>
       <!--        <field name="priority" eval="10"/>-->
       <field name="inherit_id"
              ref="sale.res_config_settings_view_form"/>
       <field name="arch" type="xml">
           <xpath expr="//div[@id='discount_sale_order_lines']"
                  position="after">
               <div class="col-12 col-lg-6 o_setting_box" id="discount_limit ">
                   <div class="o_setting_left_pane">
                       <field name="discount_limit"/>
                   </div>
                   <div class="o_setting_right_pane">
                       <label for="discount_limit" string="Discount Limit "/>
                       <div class="text-muted mb16">
                           Set Discount Limit
                       </div>
                   </div>
               </div>
           </xpath>
       </field>
   </record>
</odoo>

Adding the new boolean field to the form involves inheriting the current view. However, just our custom field will be displayed under our menu when we access the view under our custom module. The data-key attribute must be passed the model name in order to accomplish this. The main settings option will still show the same settings options.

How to Add Custom Fields to Existing Configuration Settings in Odoo 16?-cybrosys

We are unable to retrieve the values saved after saving the configuration settings since the Res Config Settings data are kept in a temporary model. As a result, it is necessary to create some additional functions for saving and retrieving saved values. For this, the get values() and set values() functions are employed. In the settings form, the get value method retrieves the saved values of configuration parameters, whereas set values save the saved values. The examples for saving and retrieving the value of a custom field are shown below.

def set_values(self):
   res = super(Confsetting, self).set_values()
   self.env['ir.config_parameter'].sudo().set_param('discount.discount_money', self.discount_money)
   self.env['ir.config_parameter'].sudo().set_param('discount.discount_limit', self.discount_limit)
   self.limit = self.env['ir.config_parameter'].get_param('discount.discount_money')
   return res
@api.model
def get_values(self):
   res = super(Confsetting, self).get_values()
   ICPSudo = self.env['ir.config_parameter'].sudo()
   res.update(
       discount_money=ICPSudo.get_param('discount.discount_money'),
       discount_limit=ICPSudo.get_param('discount.discount_limit'),
   )
   return res

Finally, a custom method or any existing method can access the value saved in Res Config Settings. The value can be gained from ir.config_parameter. For example, here, the field value is stored in a variable named discount_limit.

discount_money = fields.Float(string="Max Limit", config_parameter='discount.discount_money')

Now, adding any condition or performing any actions based on the value is possible. This way, we can create our own custom settings options under Res Config Settings.


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