odoo - 在 onchange 中将 one2many 填充到 one2many 中

标签 odoo odoo-9

使用 on_change 方法,我尝试使用 (0,0, {list value} 的概念将默认值发送到 one2many 字段;即使在那里它也工作正常,但在 one2many 内部还有一个 one2many 字段,我将还必须发送值来实现在我的代码中相同的概念(0,0,{列表值},但我没有获得成功。

你能帮我吗?

附上我的代码片段

class hc(models.Model):
    _name = 'hc'
    laboratorios_ids = fields.One2many(
        string='Laboratorios', comodel_name='resullab',
        inverse_name='hc_id')

    def cargar_pool_labs(self):
        labs = []
        pool = self.env['mymodel'].sudo().search([])
        for laboratorio in pool:
            analitos = [(0, 0, {'analito': line.analito,
                                'resultado': line.resultado ,
                                'resultado2': line.resultado2 ,
                                'unidades': line.unidades })
                            for line in laboratorio.analitos_ids]
            labs.append((0,0,{
            'nombre_examen' : laboratorio.nombre_examen,
            'cups' : laboratorio.cups,
            'laboratorio_id': laboratorio.laboratorio_id,
            'analitos_ids' : analitos
            }))
        self.laboratorios_ids = labs

注意:analitos_ids 字段是 labatorios_ids 字段内的 one2many 字段,该字段也是 one2many。

其他型号结构

class resullab(models.Model):
    _name = 'resullab'
    analitos_ids = fields.One2many('analito','laboratorio_id',
                                   'Analitos Lab', ondelete='cascade')

class labs_analito(models.Model):
    _name = 'analito'
    laboratorio_id = fields.Many2one('laboratorios', 'Lab Parent')

感谢您的帮助!

最佳答案

我在这里看到的唯一原因是analitos中缺少many2one引用。这个'analitos_ids':analitos指的是必须指定记录id的many2one模型。

def cargar_pool_labs(self):
  labs = []    
  pool = self.env['mymodel'].sudo().search([])
  for laboratorio in pool:
      analitos = [(0, 0, {'analito': line.analito, 'resultado': line.resultado , 'resultado2': line.resultado2 , 'unidades':            line.unidades, }) for line in laboratorio.analitos_ids]
      labs.append((0,0,{'nombre_examen' : laboratorio.nombre_examen,
     'cups' : laboratorio.cups, 'laboratorio_id': laboratorio.laboratorio_id.id, 'analitos_ids' : analitos}))
  self.laboratorios_ids = labs

还有一件事,many2one 引用必须是 id 而不是对象。在您的代码中,它是一个对象而不是 ID。

'laboratorio_id': laboratorio.laboratorio_id.id

Solution:

The many2one field 'hc_id' in resullab model is missing.

'hc_id' : self.id #(here the id of 'hc' model required).

analitos = [(0, 0, {'hc_id': id of resullab_object.id})]

这可能对你有帮助。

Filling default value for one2many model

关于odoo - 在 onchange 中将 one2many 填充到 one2many 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44960839/

相关文章:

javascript - Odoo-9,我的小部件在 QWeb View 中不起作用

python - 当 pip 中的链接断开时如何在 virtualenv 中安装库

python - 如何在odoo中根据另一个Many2one字段的值过滤一个Many2one字段

odoo - 现在如何通过单击按钮按日期填充字段

python - 无效的 View 定义 - Odoo v9 社区

javascript - Odoo 网络客户端。无法使用 jquery 选择器选择 html 元素

python - 当我尝试使用 odoo Controller 向我的模型添加记录时,出现此错误 "The requested URL was not found on the server"

python - 如何设置限制以允许最多选择 2 个字段?

python - 比较 OpenERP6.1 中 one2many 表中的两行

Odoo - 在 Many2one 中过滤特定组的用户