odoo res.config.settings configuration model save problem

In odoo, the res.config.settings configuration model,
the fields written by yourself, will have no value when saving and opening, this situation is more common, such as char, Many2one, Selection, Boolean, Integer and other types of fields can be directly set by config_parameter , To ensure that the value will still exist next time you open it

ebay_zip_code = fields.Char(string="Zip", default='', config_parameter='ebay_zip_code')
ebay_gallery_plus = fields.Boolean("Gallery Plus", default='', config_parameter='ebay_gallery_plus')
...

But for many-to-many Many2many field storage value is more troublesome, currently find a way to achieve, but it is not the most ideal,

Code directly

class ResConfigSettings(models.TransientModel):
    _inherit = 'res.config.settings'

    product_categ_id = fields.Many2many('product.category', string='产品类别', compute="_compute_product_categ_id", inverse="_inverse_product_categ_id")
    product_categ_str = fields.Char(config_parameter='crm.product_categ_str')

    @api.depends('product_categ_str')
    def _compute_product_categ_id(self):
        for setting in self:
            if setting.product_categ_str:
                setting_id = setting.product_categ_str.split(',')
                for rec in setting_id[0:len(setting_id)-1]:
                    product = self.env['product.category'].search([('id', '=', int(rec))])
                    setting.write({
    
    'product_categ_id':[(4, product.id, None)]})

    def _inverse_product_categ_id(self):
        str = ''
        for setting in self:
            if setting.product_categ_id:
                for rec in setting.product_categ_id:
                    str += '%s,' % rec.id
                setting.product_categ_str = str
...

It is to use the _inverse_product_categ_id method to save the id of the product_categ_id value into product_categ_str, and then assign it to product_categ_id in the compute

To put it bluntly, it is to assign the value to another field and then take it out.

This method is for reference only

Guess you like

Origin blog.csdn.net/weixin_42464956/article/details/107918089