Enable Dark Mode!
how-to-use-custom-settings-in-odoo-15.jpg
By: Sagarika

How to Use Custom Settings in Odoo 15

Technical Odoo 15

In Odoo, there are Settings options where users can set the configuration options according to their wish. Configuration Settings options provided for each application in Odoo make it more convenient for users by allowing them to configure parameters based on their business needs. Sometimes it is also possible not to use any features at all. In this blog, let’s see how we can add new settings options for a custom module and perform some operations based on the settings.

Create a custom module vehicle_rental for creating and managing rent orders for vehicles. All rental orders can be managed using the model rental.order and all rent vehicles can be managed using the model rent.vehicles.

class RentalOrder(models.Model):
   """class rental orders"""
   _name = 'rent.request'
   _descriptions = "Rental Orders"
   _inherit = ['mail.thread', 'mail.activity.mixin']
   _rec_name = 'sequence_number'
   sequence_number = fields.Char(string='Number', required=True,
                                 copy=False, readonly=True, index=True,
                                 default='New')
   customer_id = fields.Many2one('res.partner', required=True,
                                 string='Customer')
   date = fields.Date(string='Date',
                              default=fields.Date.today())
   vehicle_id = fields.Many2one('rent.vehicles', string='Vehicle',
                                ondelete='cascade', required=True,
                                force_create=False,)

Here we can add a boolean field under settings where users can either enable or disable the option to send a mail to the customer when a rental order is created for them. If the settings option is enabled, then a mail will be sent to the customer for whom the rental order is created. For adding a new custom settings option under Res Configuration Settings you have to inherit the model res.config.settings. Then add a boolean field send_mail.             

class ResConfigSettings(models.TransientModel):
   _inherit = 'res.config.settings'
   # mail send configurator
   send_mail = fields.Boolean(string="Notify Customer", default=True,
                              help="Check to Send a mail to Customer on "
                                   "creating a Rental Order")

Res Config Settings model is a model of type Transient model. A transient model has all the properties of a normal Model except that the data in a Transient model are deleted periodically. So it is not possible to use a transient model to store data.

The next step is to create a new Settings menu under the Configuration menu of our custom module. Then define the action for the menu. It is essential to pass the module name under the context attribute of the action.

<record id="res_config_settings_menu_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_id" ref="res_config_settings_view_form"/>
   <field name="view_mode">form</field>
   <field name="target">inline</field>
   <field name="context">{'module' : 'vehicle_rental'}</field>
</record>

Now lets see how to inherit settings form view and add our custom option.

<record id="res_config_settings_view_form" model="ir.ui.view">
   <field name="name">
       res.config.settings.view.form.inherit.rental
   </field>
   <field name="model">res.config.settings</field>
   <field name="priority" eval="10"/>
   <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="Vehicle Rental" data-key="vehicle_rental"
                string="Vehicle Rental">
               <h2 groups="website.group_multi_website">Notify Customer</h2>
               <div class="row mt16 o_settings_container" id="management">
                   <div class="col-12 col-lg-6 o_setting_box" id="notify_customer">
                       <div class="o_setting_left_pane">
                           <field name="send_mail"/>
                       </div>
                       <div class="o_setting_right_pane">
                           <label for="send_mail"/>
                           <div class="text-muted">
                               Check to Send a mail to Customer on creating a Rental Order
                           </div>
                       </div>
                   </div>
               </div>
           </div>
       </xpath>
   </field>
</record>

The new boolean field is added in the form by inheriting the existing view. But when we are accessing the view under our custom module, only our custom field will be shown as visible under our menu. For making this possible you have to pass the model name to the data-key  attribute. Otherwise it will show the same settings options which are shown under the main settings option.

how-to-use-custom-settings-in-odoo-15-1-cybrosys

Since Res Config Settings data are stored in a transient model we can’t get the values stored after saving the configuration settings. Hence it is required to write some extra functions for saving and getting the values saved.get_values() and set_values() methods are used for this purpose. Get value method is used to get the saved value when the settings form is loaded, where as set values store the saved values of the configuration parameters. Below given are the example for saving and getting the custom field value.

def set_values(self):
   super(ResConfigSettings, self).set_values()
   self.env['ir.config_parameter'].set_param(
       'rental.send_mail', self.send_mail)

@api.model
def get_values(self):
   res = super(ResConfigSettings, self).get_values()
   params = self.env['ir.config_parameter'].sudo()
   res.update(
       send_mail=params.get_param('rental.send_mail')
   )
   return res

Finally we can see how to access the value saved in Res Config Settings in a custom method or any existing method. The value can be gained from ir.config_parameter. As an example, here the field value is stored in a variable named send_mail.

params = request.env['ir.config_parameter'].sudo()
send_mail = params.get_param('rental.send_mail')

Now it is possible to add any condition or perform any actions based on the value. 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.



1
Comments

walid mohammedali edris

What is the best way to use setting in multiple companies

11/12/2022

-

9:30AM



Leave a comment



whatsapp
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