简体   繁体   English

动态填充下拉列表

[英]Populate DropDown List dynamically

I am trying to populate my DDL(selection) from value of another DDL in OpenERP. 我正在尝试从OpenERP中另一个DDL的值填充我的DDL(选择)。 Here is the code, that i have tried. 这是我尝试过的代码。

Here is my View XML: 这是我的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如下:

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

my onchange function is like: 我的onchange函数就像:

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 . 问题是,我没有得到过滤值my_products蒲式耳,而不是让所有的都在值my_products Plz let me know, what i am doing wrong, or point me to the right direction. 请让我知道,我做错了什么,或者指出正确的方向。 Thanks 谢谢

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 或在xml中

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

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. 我了解,首先,您必须制作一个Custom模块。

put following code in your .py file. 将以下代码放入您的.py文件中。

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 看看这个http://bazaar.launchpad.net/~openerp/openobject-addons/trunk/view/head:/product/product.py#L778

Hope this helps you. 希望这对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM