2015-04-06 45 views
0

如何使用OnChange檢索Many2one字段上的值?如何將Many2one字段中的值作爲選擇字段進行檢索?

學生應該在一個標準和一個組...標準進行登記具有多組 ,所以我想,當我改變了業界標準。該組字段應與組在標準

更新

當我嘗試這樣做,它給了我一個錯誤

'Expected singleton: fci.standard.groups(3, 4, 5, 6)' 

我想,當我改變標準領域的組字段將被更新,在此標準

只選擇組下面是我的田

'standard_id': fields.many2one('fci.standard', string='Standard', required=True), 
'group_id': fields.many2one('fci.standard.groups', string='Standard Group'), 

這裏是我的功能

def on_change_standard(self, cr, uid, ids, standard_id, context=None): 
     val = {} 
     if not standard_id: 
      return {} 
     student_obj = self.pool.get('fci.standard') 
     student_data = student_obj.browse(cr, uid, standard_id, context=context) 
     val.update({'group_id': student_data.groups_ids.id}) 
     return {'value': val} 

,這裏是我的XML

<field name="standard_id" on_change="on_change_standard(standard_id)" widget="selection"/> 
<field name="group_id" widget="selection"/> 
+0

你可以用域來做到這一點。但是你需要刪除widget =「selection」。動態域名不能用於widget =「選擇」。對於選擇小部件,動態域將被忽略。 –

回答

3

您可以編輯您的代碼,並添加域'group_id'場這樣

<field name="standard_id" widget="selection"/> 
<field name="group_id" widget="selection" domain[('standard_id','=',standard_id)]"/> 

和編輯功能上的變化這樣

def on_change_standard(self, cr, uid, ids, standard_id, context=None): 
     val = {} 
     if not standard_id: 
      return {} 
     student_obj = self.pool.get('fci.standard') 
     student_data = student_obj.browse(cr, uid, standard_id, context=context) 
     val.update({'group_id': [ g.id for g in student_data.groups_ids ]}) 
     return {'value': val} 

,它會爲工作你

bye

2

無需對這種變化的方法寫,你可以做到這一點通過域名適用於領域。試試以下,

<field name="standard_id" widget="selection"/> 
<field name="group_id" widget="selection" domain="[('standard_id','=',standard_id)]"/> 
+0

動態域名無法與widget =「選擇」一起使用。對於選擇小部件,動態域將被忽略。 –

+0

好的,然後在on_change方法中更改一件事, 'val.update({'group_id':[g.id for student_data.groups_ids]}) –

+0

我編輯它,它工作正常:D謝謝<3 –