Populate DropDown List dynamically

时光总嘲笑我的痴心妄想 提交于 2019-12-11 12:59:04

问题


I am trying to populate my DDL(selection) from value of another DDL in OpenERP. Here is the code, that i have tried.

Here is my View XML:

<h1>
    <label for="categ1" string="Parent category"/>
         <field name="categ1" on_change="Product_Category_OnChange(categ1)" />
</h1>
<newline/>
<h1> 
    <label for="my_products" string="Products" /> 
         <field name="my_products" />
</h1>

My _columns for this view is like:

_columns = {
     'categ1':fields.many2one('product.category','Parent Category',required=True),
     'my_products':fields.many2one('product.product','Products')
}

my onchange function is like:

def Product_Category_OnChange(self,cr,uid,ids,categ1):
    pro_id={}
    cr.execute('select ... where parent_id='+str(categ1))
    res = cr.fetchall()
    for i in range(len(res)):
            pro_id[i]=res[i]
    return {'domain':{'my_products': pro_id}}

The problem is, i am not getting filtered values for my_products bu instead getting all the values that are in my_products. Plz let me know, what i am doing wrong, or point me to the right direction. Thanks


回答1:


I think you want to show only the products that come under that category.

def Product_Category_OnChange(self,cr,uid,ids,categ1, context=None):
    pro_id={}
    product_obj = self.pool.get('product.category')
    if not categ1:return {}
    categ_obj = product_obj.browse(cr, uid, categ1)

    return {'domain':{'my_products':[('categ_id','=',categ_obj.id)]}}

or in xml

<field name="my_products" domain="[('categ_id','=',categ1)]" />



回答2:


Like this way. It works my side

{'domain':{'my_products':[('id','=',categ1)]}}

Syntax is like this,

def Product_Category_OnChange(self,cr,uid,ids,categ1, context=None):
        pro_id={}
        product_obj = self.pool.get('product.category')
        print product_obj.browse(cr, uid, categ1)
        cr.execute('select * from product_category where parent_id='+str(categ1))
        res = cr.fetchall()
        for i in range(len(res)):
            pro_id[i]=res[i]
        return {'domain':{'my_products':[('id','=',categ1)]}}

Update:

I understand, First of all, you have to make one Custom module.

put following code in your .py file.

class product(osv.osv):
    _inherit = 'product.product'

    def name_get(self, cr, uid, ids, context=None):
        res = []
        cr.execute('select ... where parent_id='+str(ids[0]))
        resource = cr.fetchall()
        for r in resource:
            res.append((r.id, r.name)) # My assumption
        return res

Look this http://bazaar.launchpad.net/~openerp/openobject-addons/trunk/view/head:/product/product.py#L778

Hope this helps you.



来源:https://stackoverflow.com/questions/23561387/populate-dropdown-list-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!