如何在OpenERP中使用下拉字段?

时间:2014-05-12 08:02:24

标签: python xml drop-down-menu openerp onchange

我一直在尝试使用openerp模块中的下拉菜单列表。我有一个包含类别的many2one字段。该字段与onchange函数相关联。此onchange函数将新值返回到第2个字段。现在一切正常,但我想在第二个字段中使用下拉类型菜单,以便我可以从中选择值。我的Python代码如下:

class deg_form(osv.osv):
       _name = "deg.form"
       _columns = {
             'categ1':fields.many2one('product.category','Parent Category',required=True),
             'my_products':fields.char('Product',size=64)
                  }

       def Product_Category_OnChange(self,cr,uid,ids,categ1):
           pro_id=[]
           cr.execute('select id,name from product_template where  categ_id in (select id from product_category where parent_id in (select id from product_category where parent_id='+str(categ1)+'))  union select id,name from product_template where  categ_id in (select id from product_category where parent_id='+str(categ1)+') union select id,name from product_template where categ_id='+str(categ1))
           res = cr.fetchall()
           for pid,name in res:
                pro_id.append((pid,name))
           return {'value':{'my_products':pro_id}}

这是我的xml:

 <field name="categ1" on_change="Product_Category_OnChange(categ1)" />

我的值已过滤,但都显示在以逗号分隔的字段上。我希望它们被列为下拉列表。我使用了很多2,但价值似乎没有被过滤。因此,我在下拉列表视图中实现我的值时遇到问题。请帮我。 问候

1 个答案:

答案 0 :(得分:3)

my_products应该是与product.product相关的多字段字段。

'my_products':fields.many2one('product.product',string="Product")

如果您想通过在类别上使用on_change来过滤产品列表,则必须返回my_products的新域过滤器(请使用orm方法而不是直接查询):

def Product_Category_OnChange(self,cr,uid,ids,categ1):
    product_obj = self.pool.get('product.product')
    product_ids = product_obj.search(cr, uid, [('cat_id','=',categ1)]) #maybe cat_id is wrong field name
    return {'domain':{'my_products':[('id','in',product_ids)]}}

: - )