Openerp 通过on_change方法创建和编辑one2many字段的项
我有这门课(评估) 以及本课程(注:评估) 我希望用户在选择评估表单中最后一个字段(课程id)的值时,能够通过on_change方法生成一个2多个注释评估行;并使生成的行直接显示在视图中,以便他可以插入每个note_求值行的名称值(note)。拯救一切 可能吗? 这是我当前的XML视图文件Openerp 通过on_change方法创建和编辑one2many字段的项,openerp,Openerp,我有这门课(评估) 以及本课程(注:评估) 我希望用户在选择评估表单中最后一个字段(课程id)的值时,能够通过on_change方法生成一个2多个注释评估行;并使生成的行直接显示在视图中,以便他可以插入每个note_求值行的名称值(note)。拯救一切 可能吗? 这是我当前的XML视图文件 <field name="cours_id" context="{'evaluation_id': active_id, 'test': 1}" on_change="on_change_cours_
<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>
有了它,on_change方法可以在数据库中创建note_评估,但用户界面不会加载它们,one2many字段保持为空。我观察到数据库中的注释评估没有评估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列表。但是在保存时,您可以看到旧记录和新记录。因此,取消链接将删除旧记录。另外,在添加到Notes_ID时,我使用了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)
我必须单击one2many的每一行进行编辑(以设置字段注释)。是否可以使one2many的整列都可编辑以避免逐行单击?如果您在主记录中设置注释,那么在从onchange加载one2many时,我们可以使用主记录中的注释感谢OMAL,我在模块中尝试了您的答案,保存后返回的值仅显示在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 {}
#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)
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}
(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)