Odoo:动态域过滤器仅产生一次结果

问题描述

我正在尝试使用many2one函数将域动态传递到onchange字段。它设法检索所需的结果(根据需要过滤结果),但是只执行一次。换句话说,onchange函数仅根据第一个选择成功完成了对结果的过滤,并且任何先前的选择似乎都没有影响域过滤。

代码:

class ReturnProduct(models.Model):
    _name = 'custom.return.product'
    sale_id = fields.Many2one(comodel_name="custom.sale",string="Invoice #",required=False,)
    return_line_ids = fields.One2many("custom.return.line","return_product_id",string="Products")

class ReturnLine(models.Model):
    _name = "custom.return.line"
    return_product_id = fields.Many2one("custom.return.product",string="Return Reference")
    sale_line_id = fields.Many2one("custom.sale.line",string="Product")
    reason = fields.Text(String="Return Reason")

    @api.onchange('return_product_id')
    def onchange_product_id(self):
        domain = {}
        sale_line_ids = []
        if self._context.get('sale_id'):
            sale_id = self.env["custom.sale"].browse(self._context.get('sale_id'))
            for sale_line in sale_id.product_ids.ids:
                sale_line_ids.append(sale_line)
        domain = {'sale_line_id': [('id','in',sale_line_ids)]}
        return {'domain': domain}

解决方法

我认为您需要根据销售线中的产品在退货向导中过滤产品。您需要销售系列中的产品出现在退货产品系列中

为此,您可以执行以下操作

@api.onchange('return_product_id')
def onchange_product_id(self):
    domain = {}
    product_ids = []
    if self._context.get('sale_id'):
        sale_id = self.env["custom.sale"].browse(self._context.get('sale_id'))
        for sale_line in sale_id.product_ids:
            product_ids.append(sale_line)
    domain = {'return_product_id': [('id','in',product_ids)]}
    return {'domain': domain}

实际上,您想为产品设置域。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...