top of page
Writer's pictureTek Siong, Hock

Odoo - Dynamic Domain of a Many2One field

Updated: Dec 16, 2022

If you have a requirement to restrict the selection of a Many2one field, eg, customers or vendors, based on the value of another field or other complex condition, you may use the following 2 example codes.


  1. This is a simple way to restrict the opportunities drop down values that belonged to the customer_name


opportunity_id = fields.Many2one('crm.lead', string="Opportunity")
customer_name = fields.Many2one('res.partner', string='Customer')

@api.onchange('customer_name')
def onchange_customer_name(self):
    for rec in self:
        return {'domain': {'opportunity_id': [('partner_id', '=', rec.customer_name.id)]}}


2. This is more complex way to retrieve the drop down value with logic. You will need a domain field (many2many) that will return the list of the vendors.


@api.multi
@api.depends("place_of_delivery")
def _get_vendor_domain(self):
    for obj in self:
        vendor_ids = []
        for line in obj.place_of_delivery.haulage_charge_line_ids:
            for vendor_id in line.vendor_id.ids:
                vendor_ids.append(vendor_id)
            self.vendor_list = [(6,0, vendor_ids)]

vendor = fields.Many2one('res.partner', string="Vendor")
vendor_list = fields.Many2many('res.partner',store=True,compute=_get_vendor_domain)


<field name="vendor_list" invisible="1"/>
<field name="vendor" domain="[('id','in',vendor_list)]"/>

Note that there is a web domain field to do that as well.

2,028 views0 comments

Recent Posts

See All

Kommentare


bottom of page