问题描述
我正在尝试使用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}
实际上,您想为产品设置域。