2014-01-06 34 views
5

我有這個類(評價)通過on_change方法

class schoolem_evaluation(osv.Model): 
_name = 'schoolem.evaluation' 
_columns = { 
    'name' : fields.char('Evaluation',help="Champ automatique = periodeN_ExamenN_CoursX_SalleDeClasseS"), 
    'aca_id' : fields.many2one('schoolem.aca','Annee Academique',required=True), 
    'periode_id' : fields.many2one('schoolem.periode','Periode',required=True,help="Par exemple : trimestre01"), 
    'examen_id' : fields.many2one('schoolem.examen','Examen',required=True), 
    'salle_de_classe_id' : fields.many2one('schoolem.salle_de_classe','Salle de Classe',required=True), 
    'cours_id' : fields.many2one('schoolem.cours','Cours',required=True), 
    'note_ids' : fields.one2many('schoolem.note_evaluation','evaluation_id','Notes'), 
} 

這個類(note_evaluation)

class schoolem_note_evaluation(osv.Model): 
    _name = 'schoolem.note_evaluation' 
    _order = 'etudiant_id' 
    _columns = { 
     'name' : fields.float('Note',digits=(6,2),required=True), 
     'evaluation_id' : fields.many2one('schoolem.evaluation','Evaluation',), 
     'etudiant_id' : fields.many2one('schoolem.etudiant','Etudiant',required=True), 
     'rang' : fields.integer('Rang'), 
     'observation' : fields.text('Observation'), 
    } 

而且我希望用戶能夠創建和編輯one2many領域的項目在選擇Evaluation_form中的最後一個字段(cours_id)的值時,通過on_change方法生成one2many note_evaluation行;並使生成的直接直接出現在視圖中,以便他可以插入每個note_evaluation行的名稱值(note)。並保存所有。

可能嗎? 這是我目前的XML視圖文件

<field name="cours_id" context="{'evaluation_id': active_id, 'test': 1}" on_change="on_change_cours_id(examen_id,salle_de_classe_id,cours_id,aca_id)"/> 
        </group> 
        <notebook> 
         <page string="Inserer des notes"> 
          <field nolabel="1" name="note_ids" context="{'evaluation_id': active_id}"/> 
         </page> 

,這是平變化功能:

def on_change_cours_id(self,cr, uid, ids,examen_id,salle_de_classe_id,cours_id,aca_id,context=None): 

     context=context or {} 

     #if context is None: 

     # context = {} 
       for etud_id in etudiant_ids: 

        note_eval = self.pool.get('schoolem.note_evaluation') 

        if not context.get('id', False): #id de l'evaluation 

         context['id'] = context.get('evaluation_id') 

        eval_id = context.get('id', False) 
        raise osv.except_osv(('the form!'), (context.get('active_id'))) 

        id = note_eval.create(cr, uid, {'name':0,'etudiant_id':etud_id,'evaluation_id':eval_id}, context=context) 

據此,該on_change方法在數據庫中創建的note_evaluation但用戶界面不加載它們並且二十一個字段保持空白。我觀察到數據庫中的note_evaluation沒有評估標識。

怎麼辦?

回答

18

要使用onchange加載one2many數據,您不必從onchange函數創建one2many數據。您只需要將值創建到one2many字段。例如

def on_change_cours_id(self,cr, uid, ids,examen_id,salle_de_classe_id,cours_id,aca_id,context=None): 
    context=context or {} 
    note_ids = [] 
    value = {} 
    if ids: 
     old_note_ids = self.pool.get('schoolem.note_evaluation').search(cr, uid,[('evaluation_id','in',ids)]) 
     self.pool.get('schoolem.note_evaluation').unlink(cr, uid, old_note_ids) 
    etudiant_ids = [] 
    #search for etudiant_ids with the conditions examen_id,salle_de_classe_id,cours_id,aca_id etc 
    for etud_id in etudiant_ids: 
     note_ids.append((0,0,{'name':0,'etudiant_id':etud_id})) 
    value.update(note_ids=note_ids) 
    return {'value':value} 

在這裏,我已經做了取消鏈接,因爲當您保存該記錄,並再次以某種方式加載的onchange,新one2many列表將顯示。但是當一個保存時,你可以看到舊的記錄和新的一個。SO取消鏈接將刪除舊的記錄。當附加到note_ids時,我已經使用了帶有0,0,{values}的元組,下面是該解釋

(0, 0, { values }) link to a new record that needs to be created with the given values dictionary 
(1, ID, { values }) update the linked record with id = ID (write *values* on it) 
(2, ID)    remove and delete the linked record with id = ID (calls unlink on ID, that will delete the object completely, and the link to it as well) 
(3, ID)    cut the link to the linked record with id = ID (delete the relationship between the two objects but does not delete the target object itself) 
(4, ID)    link to existing record with id = ID (adds a relationship) 
(5)     unlink all (like using (3,ID) for all linked records) 
(6, 0, [IDs])   replace the list of linked IDs (like using (5) then (4,ID) for each ID in the list of IDs) 
+1

根據規則,我不能說非常感謝,但... – levolutionniste

+0

我必須點擊one2many的每一行來編輯(設置字段註釋)。是否可以使one2many的整個列可編輯以避免逐行點擊? – levolutionniste

+0

如果你在主記錄中設置記錄,那麼在從onchange加載one2many時,我們可以使用主記錄 – OmaL