This question has been flagged

Im unable to set domain on many2one field via onchange function of another field.

For example i was checking if this function works in purchase.py file.

xml of view

<field name="partner_id" on_change="onchange_partner_id(partner_id)" context="{'search_default_supplier':1,'default_supplier':1,'default_customer':0}" domain="[('supplier','=',True)]"/>

def onchange_partner_id(self, cr, uid, ids, partner_id):

    partner = self.pool.get('res.partner')
    if not partner_id:
        return {'value': {
            'fiscal_position': False,
            'payment_term_id': False,
            },'domain':{'product_id':[('purchase_ok','=',True)]}}
    supplier_address = partner.address_get(cr, uid, [partner_id], ['default'])
    supplier = partner.browse(cr, uid, partner_id)
    return {'domain':{'product_id':[('purchase_ok','=',True)]},'value': {
        'pricelist_id': supplier.property_product_pricelist_purchase.id,
        'fiscal_position': supplier.property_account_position and supplier.property_account_position.id or False,
        'payment_term_id': supplier.property_supplier_payment_term.id or False,
        }}

The domain on field has been removed and i included that in previous function 'product_id': fields.many2one('product.product', 'Product', change_default=True)

My question is, the domain which i set on function is not reflecting when i click on product_id field. I am getting all products as list rather than purchasable products. It would be nice to know if someone points out what i'm missing.

Also should we set anything more on product_id field view? Presently it looks like this <field name="product_id" on_change="onchange_product_id(parent.pricelist_id,product_id,0,product_uom,parent.partner_id, parent.date_order,parent.fiscal_position,date_planned,name,price_unit,context)"/>

PS: Im just trying out how domain works with onchange function so i just did this minor change in core module to understand the concept. I know it isn't advisable to change core modules.

Thank you

Avatar
Discard

Did you find any solution?

Can you try out like below
@api.onchange('partner_id')
def onchange_partner_id(self):
domain = []
if self.partner_id:
domain = [('partner_id', '=', self.partner_id.id)]
return {'domain': {'many2one_field': domain}}