Creating Dynamic Many2One field with onchange function in OpenERP


This post will help in creating many2one field whose value changes when the content of another field changes.

Lets consider a simple example. Suppose we have a many2one field to Product (name: product_id). We now want to change the content on this many2one field based on type selection box(name: type). Now, if I choose type as Service, only service type product should be visible in Product field.

To accomplish this, we have to define on change function on type selection box.

def onchange_type(self,cr,uid,ids, type,context=None):
    product_obj = self.pool.get('product.product')
    product_ids = product_obj.search(cr,uid, [('type','=',type)])
    return {'domain':{'product_id':[('id','in',product_ids)]}}

8 thoughts on “Creating Dynamic Many2One field with onchange function in OpenERP

  1. Hello, i am trying to modify the ‘internal reference’ (default_code) of a product so that when i start typing, it displays a list of existing references that start with what i have already typed? Can this be done? I assume i have to define the onchange, but as i’m a beginner with odoo, i have no idea what to write in it. Thank you

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s