Enable Dark Mode!
how-to-save-many2many-field-in-odoo-13-settings.png
By: Saurabh PV

How to Save Many2many Field in Odoo 13 Settings

Technical Odoo 13

The many2many fields in Odoo are similar in operation with the many2one fields, however, with the capability to select multiple records at once. An example for the operation is selecting mulstsipetags for a vendor in the Odoo operations which is achievable with the help of many2many field operations.

In this blog, we will discuss how to create and save a many2many field in the settings model ‘res.config.settings’. 

For creating fields in any other model, kindly refer to the following blog on How to Add Custom Fields to Existing Views in Odoo v12.

The operation of many2many fields can be well explained with the help of an example here, we will bring a many2many field having records of the model ‘fleet.vehicle’ to the settings page. 

Firstly, we define the field in the python file and its corresponding views in the XML file to understand how it's done.


Python file


from odoo import models, fields, api from ast import literal_eval class ResConfigSettings(models.TransientModel): _inherit = 'res.config.settings' company_vehicles = fields.Many2many('fleet.vehicle', string="Company Speific Vehicles")

Now let's understand the XML file operation

XML file


<?xml version="1.0" encoding="utf-8"?> <odoo> <data> <record id="res_config_settings_view_form_many2many" model="ir.ui.view"> <field name="name">res.config.settings.view.form.inherit.hr.fleet.many2many </field> <field name="model">res.config.settings</field> <field name="priority" eval="90"/> <field name="inherit_id" ref="base.res_config_settings_view_form"/> <field name="arch" type="xml"> <xpath expr="//div[@id='fleet']" position="inside"> <div class="row mt16 o_settings_container"> <div class="col-12 col-lg-6 o_setting_box"> <div class="o_setting_left_pane"/> <div class="o_setting_right_pane"> <span class="o_form_label">Company Vehicles</span> <div class="text-muted content-group mt16"> <field name="company_vehicles" widget="many2many_tags"/> </div> </div> </div> </div> </xpath> </field> </record> </data> </odoo>

After configuring the Python and the XML files you will be now able to view a new many2many field called “Company Vehicles” in the settings of the Fleet management module.


how-to-save-many2many-field-in-odoo-13-settings


You will be able to select the values here but it won’t be saved and be left blank when you click the save button. Therefore to save the values in many2many you should write two additional functions.


Add these two functions to the python file

set_values method def set_values(self): res = super(ResConfigSettings, self).set_values() self.env['ir.config_parameter'].sudo().set_param('many2many.company_vehicles', self.company_vehicles.ids) return res

Additionally, here in the set_values method, we inherit the  set_values() method of class ResConfigSettings and add an extra line in the following format, 


self.env['ir.config_parameter'].sudo().set_param('your_module_name.your_many2many_field', self.your_many2many_field.ids)

Moreover, here in the set_param method call, parameters are,


your_module_name is the name of your custom module,

your_many2many_field is the name of your custom many2many field

Your_many2many_field.ids is used for the multiple records passed from the many2many field


get_values method @api.model def get_values(self): res = super(ResConfigSettings, self).get_values() with_user = self.env['ir.config_parameter'].sudo() com_vehicles = with_user.get_param('many2many.company_vehicles') res.update( company_vehicles=[(6, 0, literal_eval(com_vehicles))] if com_vehicles else False, ) return res


Furthermore, in get_values method, we inherit the get_values() method similar to the inheritance of set_values() method,

Here we assign the self.env['ir.config_parameter'].sudo() to a variable with_user and using with_user we call the method get_param using the parameters in the following format. It is then assigned  to another variable com_vehicles,


with_user = self.env['ir.config_parameter'].sudo() com_vehicles = with_user.get_param(your_module_name.your_many2many_field)


The above command is used to fetch the existing values at our many2many field,


res.update( company_vehicles=[(6, 0, literal_eval(com_vehicles))] if com_vehicles else False,


Additionally, using the code above, it will load the values we previously saved in our many2many field.

Furthermore, a condition if com_vehicles else False, is also provided to check and ignore the code when our field is left empty.


how-to-save-many2many-field-in-odoo-13-settings


After adding these set_values and get_values methods to the python code, you will be able to save the values set in your many2many field inside settings. The above image depicts the vehicles being saved in the fleet management settings window as company vehicles.



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



1
Comments

Hiren Dangar

Better to define company_vehicles M2m fields in res.company model and take related field into res.config.settings model so it's easy to use in company dependent.

06/03/2020

-

4:28AM



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