问题
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