odoo res.config.settings配置模型保存问题

107 篇文章 4 订阅
66 篇文章 4 订阅

在 odoo 中,res.config.settings配置模型,
自己写的字段,会产生保存打开没有值的情况,这种情况比较常见,像char,Many2one,Selection,Boolean ,Integer等类型的字段可以直接 config_parameter设置,保证下次打开时,值还是存在的

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')
...

但是 对于 多对多 Many2many 字段的存值就比较麻烦,目前找到一种方式可以实现,但不是最理想的,

直接上代码

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

就是用 _inverse_product_categ_id 方法保存的是把 product_categ_id 值的 id 存到 product_categ_str 里面,然后在 compute 里面再赋值给 product_categ_id

说白了,就是把值赋给另一个字段,然后取出来。

此方法仅供参考

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

隔壁小红馆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值