Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用值[ODOO 12]更新One2many列表_Python_Append_Updates_Odoo 12 - Fatal编程技术网

Python 如何使用值[ODOO 12]更新One2many列表

Python 如何使用值[ODOO 12]更新One2many列表,python,append,updates,odoo-12,Python,Append,Updates,Odoo 12,我添加了一种方法,在课堂上对老师进行研究,然后他在院子里添加(课程有几节课,每节课一位老师) 我的问题是,当我点击按钮时,它不会更新表,他在每次点击下面都添加了另一行 这是我的密码 teacher_ids = fields.One2many('school.teacher', 'course_id', string='Teacher') def get_teachers (self): lesson = self.env['school.lesson'].sea

我添加了一种方法,在课堂上对老师进行研究,然后他在院子里添加(课程有几节课,每节课一位老师) 我的问题是,当我点击按钮时,它不会更新表,他在每次点击下面都添加了另一行

这是我的密码

    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 
我发现

(6, 0, [IDs])          replace the list of linked IDs (like using (5) then (4,ID) for each ID in the list of IDs)
但我不知道在我的方法中是如何使用的

您不能在one2many字段中使用(6,0,[ID])。因为他说

  • (4,身份证) 将id为的现有记录添加到集合中。无法在One2many上使用。
  • (五,十,十) 从集合中删除所有记录,相当于对每个记录显式使用命令3。不能在One2many上使用。无法在create()中使用
你应该用这个来代替

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

self.teacher_ids = [(6, 0, [])]
然后更新为

self.write({'teacher_ids' : teacher_list})
它将在以下方面发挥作用:

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 

谢谢@Terrence的回答我会测试回来不起作用,我需要做一个表格更新,即当我点击申请“get_teachers”时,它会搜索并在One2中添加教师。在我的情况下,他会做研究,但他不会抑制最近的记录。我需要替换以前的参考