You have a little problem here: you can't set a domain for child relations IIRC.
What you can do is to create a related field on the child relation (here purchase.order.line
and trigger the onchange on that.
class PurchaseOrder(models.Model):
_inherit = "purchase.order"
custom_id = fields.Many2one(
comodel_name='custom.model', string='Custom')
class PurchaseOrderLine(models.Model):
_inherit = "purchase.order.line"
custom_id = fields.Many2one(
comodel_name="custom.model", related="order_id.custom_id")
@api.onchange('custom_id')
def onchange_custom_id(self):
lst = [1,2,3]
return {'domain': {'product_id': [('id', 'in', lst)]}}
And some more information. Let me presume you have set custom_id
on product variants (a) or on product categories (b). Just change the domain of product_id
and use the domain operator '=?'. You won't need an onchange method for this solution, but you have to define custom_id
in the view (invisible is possible, this is also a must have for the complete first solution)!
(a)
class PurchaseOrderLine(models.Model):
_inherit = "purchase.order.line"
custom_id = fields.Many2one(
comodel_name="custom.model", related="order_id.custom_id")
product_id = fields.Many2one(
domain="[('purchase_ok', '=', True), ('custom_id', '=?', custom_id)]")
(b)
class PurchaseOrderLine(models.Model):
_inherit = "purchase.order.line"
custom_id = fields.Many2one(
comodel_name="custom.model", related="order_id.custom_id")
product_id = fields.Many2one(
domain="[('purchase_ok', '=', True), ('categ_id.custom_id', '=?', custom_id)]")
If no custom_id is set the domain will read: product has to be purchasable AND True.