This question has been flagged

I was trying to make two many2many fields inside configuration settings of website. I could make both fields fields but I was not able to save the multiple entered values.  This is the error that occured


 File "/home/RobertD'Souza/Desktop/odoo-13.0/odoo/models.py", line 1688, in _add_missing_default_values     if self._fields[name].type == 'many2many' and value and isinstance(value[0], int):
KeyError: 'web_products'



And this is the code I used


class ResConfigSettings(models.TransientModel):
_inherit = 'res.config.settings'
web_product = fields.Many2many('product.template', string="Website Products")
web_category = fields.Many2many('product.public.category', string="Website Categories")

def set_values(self):
res = super(ResConfigSettings, self).set_values()
self.env['ir.config_parameter'].set_param('website_product_visibility.web_product', self.web_product)
self.env['ir.config_parameter'].set_param('website_product_visibility.web_category', self.web_category)
return res

@api.model
def get_values(self):
res = super(ResConfigSettings, self).get_values()
ICPsudo = self.env['ir.config_parameter'].sudo()
web_product = ICPsudo.get_param('website_product_visibility.web_product')
web_category = ICPsudo.get_param('website_product_visibility.web_category')
res.update(
web_products=web_product,
web_categories=web_category
)
return res
Avatar
Discard
Best Answer

Hi,

You can try this code.

class XYZConfigSettings(models.TransientModel):
_name = 'xyz.config.settings'
_inherit = 'res.config.settings'

product_ids = fields.Many2many('product.product', string="Products")

@api.model
def get_default_values(self, fields):
IrValues = self.env['ir.values'].sudo()
product_ids = IrValues.get_default('xyz.config.settings', 'product_ids')
lines = False
if product_ids:
lines = [(6, 0, product_ids)]
return {
'product_ids': lines,
}

@api.multi
def set_default_values(self):
IrValues = self.env['ir.values'].sudo()
IrValues.set_default('xyz.config.settings', 'product_ids', self.product_ids.ids)

For adding settings and saving values in settings, see: How To Add Settings/Configuration For Module in Odoo

To add many2many Field in the Settings in Odoo12 See this: How To Save Many2many Field Value In Settings Odoo

Thanks


Avatar
Discard
Author

Thank you for answering Niyas, unfortunately it does not work as most of your code is written in Odoo 10 and I'm doing in Odoo 13. I've tried changing ir.values to ir.default and other necessary changes to fields but still not working.

Author

Thank you so much, this was what I needed.

Best Answer

Hi,

You can refer the blog below for storing a many2many field in settings of Odoo 13

https://www.cybrosys.com/blog/how-to-save-many2many-field-in-odoo-13-settings

Regards

Avatar
Discard
Best Answer

Hi,

   Refer this https://www.odoo.com/forum/help-1/question/how-to-get-and-set-a-many2many-field-in-res-config-settings-odoo-12-159437        Make changes accordingly   

Avatar
Discard
Author

It is working fine with a text field, the problem only arises during the usage of many2many fields.