【问题标题】:Odoo: Values of Many2many with dynamic domain aren't getting savedOdoo:具有动态域的 Many2many 的值没有得到保存
【发布时间】:2020-09-13 13:33:36
【问题描述】:

我正在尝试根据其他字段的多个onchange 函数(例如brand_idorigin_id)动态更改many2many 字段products_ids 的值。 到目前为止,一切都很好,它确实显示了预期值,但是一旦我点击保存按钮,many2many 字段的值就会消失

class CustomModifyPrice(models.Model):
    brand_id = fields.Many2many(comodel_name="custom.brand", string="Brand", required=False, )
    origin_id = fields.Many2many(comodel_name="custom.country", string="Origin", required=False, )
    product_ids = fields.Many2many(comodel_name="custom.product", string="Products", readonly=True, )
    search_terms = {}
    product_ids_list = []

    @api.onchange('brand_id')
    def onchange_change_brand(self):
        for rec in self:
            product_brands = []
            for prod_brand in rec.brand_id:
                product_brands.append(prod_brand.id)
            rec.search_terms["product_brands"] = product_brands
            rec.get_products()

    @api.onchange('origin_id')
    def onchange_change_origin(self):
        for rec in self:
            product_origins = []
            for prod_origin in rec.origin_id:
                product_origins.append(prod_origin.id)
            rec.search_terms["product_origins"] = product_origins
            rec.get_products()

    def get_products(self):
        domain = []
        self.product_ids_list = []
        if 'product_brands' in self.search_terms:
            product_brands = self.search_terms['product_brands']
            if product_brands:
                tuple1 = ('brand_id', 'in', product_brands)
                domain.append(tuple1)

        if 'product_origins' in self.search_terms:
            product_origins = self.search_terms['product_origins']
            if product_origins:
                tuple1 = ('country_id', 'in', product_origins)
                domain.append(tuple1)

        if domain:
            products = self.env['custom.product'].search(domain)
            if products.ids:
                for prod in products:
                    self.product_ids_list.append(prod.id)
            self.product_ids = [(6, False, self.product_ids_list)]

【问题讨论】:

  • 你的字段是只读的,尝试使用force_save
  • 我完全同意 Kenly。在您的 xml 视图中使用 force_save=1。

标签: python odoo-12


【解决方案1】:

确保将force_save="1" 作为属性放置在您的字段(xml 文件)中

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多