2014-05-12 21 views
1

我一直在嘗試在我的openerp模塊中使用下拉菜單列表。我有一個many2one字段包含類別。該字段與一個onchange函數關聯。這個onchange函數返回第二個字段的新值。現在一切正常,但我想在第二個字段中使用下拉菜單,以便我可以從中選擇值。我的Python代碼如下:如何在OpenERP中使用下拉字段?

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)" /> 

我的價值觀,但都顯示在用逗號分隔的領域。我希望他們被列爲下拉列表。我用many2one,但這些值似乎沒有被過濾。因此,我在實現我的值作爲下拉列表視圖時遇到問題。請幫幫我。 Regards

回答

3

my_products應該是與product.product有關的many2one字段。

'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)]}} 

: - )

+0

我必須使用查詢,因爲我有要求加入其他表的數據的要求。無論如何,我可以使用查詢來做到這一點。謝謝 –

+0

ofcourse:使用您的查詢並創建一個所有(結果)ID列表並使用該域的新列表。像[('id','in',your_id_list]] – CZoellner

相關問題