gpt4 book ai didi

openerp - 通过 on_change 方法创建和编辑 one2many 字段的项目

转载 作者:行者123 更新时间:2023-12-04 16:25:57 29 4
gpt4 key购买 nike

我有这门课(评价)

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

并且我希望用户在选择 Evaluation_form 中的最后一个字段 (cours_id) 的值时,能够通过 on_change 方法生成 one2many note_evaluation 行;并使生成的行直接出现在 View 中,以便他可以插入每个note_evaluation行的名称值(note)。并保存所有。

是否可以?
这是我当前的 XML View 文件
<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>

这是 onchange 函数:
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 但用户界面不会加载它们并且 one2many 字段保持为空。我观察到数据库中的 note_evaluation 没有evaluation_id。

怎么做?

最佳答案

要使用 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 unlink 将删除旧记录。此外,在附加到 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)

关于openerp - 通过 on_change 方法创建和编辑 one2many 字段的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20954412/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com