【问题标题】:how to update One2many list with value [ODOO 12]如何使用值更新 One2many 列表 [ODOO 12]
【发布时间】:2019-11-18 11:22:10
【问题描述】:

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

这是我的代码

    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)

但我不知道在我的方法中如何使用

【问题讨论】:

    标签: python append updates odoo-12


    【解决方案1】:

    先放

    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 
    

    【讨论】:

      【解决方案2】:

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

      • (4,身份证,_) 将 id id 的现有记录添加到集合中。不能在 One2many 上使用。
      • (5, _, _) 从集合中删除所有记录,相当于在每条记录上显式使用命令 3。不能在 One2many 上使用。不能在 create() 中使用。

      你应该用这个替换

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

      【讨论】:

      • 谢谢@Terrence 的回答,我会测试并回来
      • 不起作用,我需要制作一个表格 update 即当我单击应用“get_teachers”时,它会搜索并添加 One2many 中的教师,在我的如果他做了研究,但他没有隐瞒最近的记录
      • 我需要替换以前的引用
      猜你喜欢
      • 1970-01-01
      • 2020-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多