Python 动态填充下拉列表
我试图从OpenERP中另一个DDL的值填充我的DDL(选择)。这是我尝试过的代码 以下是我的XML视图:Python 动态填充下拉列表,python,openerp,onchange,openerp-7,Python,Openerp,Onchange,Openerp 7,我试图从OpenERP中另一个DDL的值填充我的DDL(选择)。这是我尝试过的代码 以下是我的XML视图: <h1> <label for="categ1" string="Parent category"/> <field name="categ1" on_change="Product_Category_OnChange(categ1)" /> </h1> <newline/> <h1>
<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>
我的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}}
问题是,我没有得到
my_products
bu的筛选值,而是得到my_products
中的所有值。请告诉我,我做错了什么,或者给我指出正确的方向。这样谢谢你。我这边行
{'domain':{'my_products':[('id','=',categ1)]}}
语法是这样的
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)]}}
更新:
我理解,首先,您必须制作一个自定义模块
将以下代码放入.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
看这个
希望这对您有所帮助。我想您只想展示属于该类别的产品
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)]}}
或者使用xml
<field name="my_products" domain="[('categ_id','=',categ1)]" />
您可以在xml或py中使用域过滤器为什么要在更改时使用域过滤器?我在py文件和xml中都使用域过滤器,但实际上我必须在查询中使用域过滤器,因为模型中存在一些自连接和父子关系。这就是为什么我要用on_change
方法来做这件事。任何建议都有错。如果您详细说明或发布一些链接,以便我能有一些想法通过@Dhanac检查答案,我们可以创建一个下拉字段,因为我的值正在返回,但它们都与ID一起显示在1个文本框中。您是否修改了产品的name\u get函数我使用了cr.execute在我的onchange中运行sql查询函数返回值。这些值是可以的,但要实现它们,可以使用widget=“selection”作为下拉菜单或选择类型菜单。但是我不知道为什么它会在下拉列表中显示ID我的代码中没有下拉列表。当我使用manyOne时,它会给我TypeError:object29没有“split”方法,实际上我想从我发布的查询中填充我的_产品。我有一个要求,我应该直接从查询中获取我的产品的数据,而不是使用categ1 id进行过滤。我希望现在已经清楚了。谢谢
<field name="my_products" domain="[('categ_id','=',categ1)]" />