Odoo:许多具有dynamic domain的值未被保存

2024-05-14 07:37:06 发布

您现在位置:Python中文网/ 问答频道 /正文

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

enter image description here

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)]

Tags: inselfididssearchifprodorigin

热门问题