how-to-save-many2many-field-in-odoo-13-settings.png
Blogger_637067939133517473.png
By: Saurabh PV

How to Save Many2many Field in Odoo 13 Settings

Technical Odoo 13


In this blog, we will discuss how to create and save a many2many field in settings model ‘res.config.settings’. For creating fields in any other model, kindly refer to this blog on How to Add Custom Fields to Existing Views in Odoo v12. 


In this example, we will bring a many2many field having records of the model ‘fleet.vehicle’ to the settings page. 

First, we define the field in the python file and its corresponding views in the XML file.


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")



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>


Now you will be able to see a new many2many field called “Company Vehicles” in the settings of the Fleet.


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


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)


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 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


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)


This 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,


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

A condition

if com_vehicles else False,

is also provided to check and ignore the code when our field is left empty


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.


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




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


cybrosys youtube

Comments

1

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

 
Calicut

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

London

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

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message