3

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

Boubaker
  • 427
  • 1
  • 7
  • 24

2 Answers2

3

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 
vvvvv
  • 25,404
  • 19
  • 49
  • 81
Akshay
  • 639
  • 3
  • 8
0

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])])
Terrence Poe
  • 634
  • 4
  • 17
  • thank you @Terrence for your answer I will test and come back – Boubaker Jul 09 '19 at 08:18
  • does not work, my need is to make a table **update** ie when I click to apply 'get_teachers' it does a search and add the teachers in the One2many, in my case he does the research but he does not suppress the recent records – Boubaker Jul 09 '19 at 18:00
  • I need replace previous references – Boubaker Jul 09 '19 at 18:21