Search code examples
pythonappendupdatesodoo-12

how to update One2many list with value [ODOO 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


Solution

  • 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