how to update One2many list with value [ODOO 12]

让人想犯罪 __ 提交于 2021-02-19 03:53:12

问题


I added a method that does a research of the teacher in the lessons then he added in the yard (the course several lesson, each lesson a single teacher) my problem is when I click on the button it does not the table update, he has added another line below each click

it's my code

    teacher_ids = fields.One2many('school.teacher', 'course_id', string='Teacher')

    def get_teachers (self):
        lesson = self.env['school.lesson'].search([])

        teacher_list = []  
        for rec in lesson:
            if rec.course_id.id == self.id:
                print(rec.teacher_id.name)

                teacher_list.append([0,0,{
                                    'teacher_name':  rec.teacher_id.id,  
                                    'lesson_id': rec.id,
                                }])
        print('teacher_list',teacher_list)
        self.write({'teacher_ids' : teacher_list})
        return 

I found that

(6, 0, [IDs])          replace the list of linked IDs (like using (5) then (4,ID) for each ID in the list of IDs)

but I do not know how used in my method


回答1:


Firstly put

self.teacher_ids = [(6, 0, [])]

then update with

self.write({'teacher_ids' : teacher_list})

It will work:

def get_teachers (self):
    lesson = self.env['gestcal.lesson'].search([])

    teacher_list = [] 
    for rec in self.lesson_id:
        teacher_list.append([0,0,{
                                'teacher_name':  rec.teacher_id.id,  
                                }])
    print('teacher_list',teacher_list)
    self.teacher_ids = [(6, 0, [])]
    self.write({'teacher_ids' : teacher_list})
    return 



回答2:


you can not use (6, 0, [IDs]) in one2many field. Since official document says.

  • (4, id, _) adds an existing record of id id to the set. Can not be used on One2many.
  • (5, _, _) removes all records from the set, equivalent to using the command 3 on every record explicitly. Can not be used on One2many. Can not be used in create().

you should replace by this

self.teacher_ids = self.env['your_teachers_model'].search([('id', 'in', [(rec.id) for rec in lesson])])


来源:https://stackoverflow.com/questions/56943475/how-to-update-one2many-list-with-value-odoo-12

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!