Enable Dark Mode!
By: Prayag K

How to Save Many2many Field in Odoo V15 Settings

Technical Odoo 15

The many2many fields in Odoo work in a similar way to the many2one fields, except they allow you to pick numerous entries at once. So that you can easily link numerous data of any table with the help of this many2many field.

For an example of many2many fields in odoo, you can refer to the below image. Here the field Tags is a many2many field inside Contacts.


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

So, in this blog, the operations of many2many fields can be explained using an example. We will add a many2many field on the settings page with the records from the model 'res.partner.'

First, we need to define a many2many fields in the Python file. And its corresponding views in the XML file.

Python file

from odoo import api, models, fields
from ast import literal_eval
class ResConfSettings(models.TransientModel):
_inherit = 'res.config.settings'
company_contacts = fields.Many2many('res.partner', string="Company Contacts")

XML file

<?xml version="1.0" encoding="utf-8"?>
    <record id="res_config_settings_view_form_many2many" model="ir.ui.view">
        <field name="name">
        <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[hasclass('settings')]" 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 Contacts
                            <div class="text-muted content-group mt16">
                                <field name="company_contacts"

After configuring the Python and XML files, just install or upgrade the module. Then when you check the Odoo Settings, You can view the Many2many field “Company Contacts.”


Here you can select the data you need, but the problem is when you click the Save button, the field becomes blank. As a result, two more functions in the Python file are required to save the data in the many2many fields in the settings.

set_values method

def set_values(self):
    res = super(ResConfigSettings, self).set_values()
        'module_name.company_contacts', self.company_contacts.ids)
    return res

Here we are inheriting the method set_values from the class ResConfSettings. 

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

The code above is a call for the set_param method, the parameters are

Your custom module name,

Your many2many field,

Your many2many field.ids

get_values method

def get_values(self):
    res = super(ResConfigSettings, self).get_values()
    with_user = self.env['ir.config_parameter'].sudo()
    com_contacts = with_user.get_param('module_name.company_contacts')
    res.update(company_contacts=[(6, 0, literal_eval(com_contacts))
                                  ] if com_contacts else False, )
    return res

Here in the get_values method, we inherit the get_values method of class ResConfigSettings.

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

Then we assign self.env['ir.config_parameter'].sudo() to a variable with_user. And using the variable with_user call the get_param method with the parameters custom_module_name.many2many_field_name. Those values are then assigned to a variable com_contacts. This fetches the existing values in our many2many fields.

res.update(company_contacts=[(6, 0, literal_eval(com_contacts))
                                  ] if com_contacts else False, )

This line of code fetches and loads the values we previously saved in our many2many fields.

And also, check for the condition if com_contacts is False or has any value. If it is False, the code will be ignored and left the field empty.


After adding these two functions to our Python file, you will be able to save the corresponding data to the many2many fields. The above image depicts the contacts being saved in the settings window as company contacts.

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


Leave a comment




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



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



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

Send Us A Message