为什么我的 One2Many 字段保存的记录不超过一条?

Why my One2Many field does not save more than one record?

我有两个模型。一个模型有一个 One2Many 字段指向另一个模型。当我保存主记录时,它只保存 One2many 关系的一条记录。我需要它来保存 'n' 条记录。可能是我规划的数据结构有问题

这是代码。对于变量的西班牙名称,我很抱歉。

提前致谢。

这是第一个 class 它调用第二个 class 与 One2Many 关系。

class EmpleadosProductos(models.Model): _name = "employee.as.product"

    #the One2Many relation of the trouble
    employee_line = fields.One2many(
        'employee.line', 
        'id', 
        string='Employee Lines',
        store=True
        )

    companyias = fields.Many2one('res.partner', 'Compañia', domain=[('is_company', '=', True)])
    amount_total = fields.Monetary(string='Total', store=True, readonly=True, compute='_calcularTotal')
    journal_entry = fields.Many2one('account.move')
    currency_id = fields.Many2one('res.currency', 'Currency', required=True, default=lambda self: self.env.user.company_id.currency_id.id)
    fecha = fields.Date('Fecha')
    referencia = fields.Char(string='Ref', required=True) 
    _sql_constraints = [
        ('refererecia_constraint', 'unique(referencia)', 'La referencia debe de ser única!'),
    ]
    @api.one
    @api.depends('employee_line.total')
        def _calcularTotal(self):
            for empleado_obra in self:
                acumulador = 0.0
                for linea in empleado_obra.employee_line:
                    acumulador += linea.total
                empleado_obra.update({
                    'amount_total': acumulador
                })

这是 One2Many 关系中调用的第二个 class。

class EmployeLine(models.Model):
    _name = 'employee.line'
    descripcion = fields.Text(string='Descripción', required=False)
    employee_id = fields.Many2one(
        'hr.employee', 
        string="Empleado",
        requiered=True,
        change_default=True
        )

    currency_id = fields.Many2one('res.currency', 'Currency', required=True, default=lambda self: self.env.user.company_id.currency_id.id)
    apodo = fields.Char('apodo', readonly=False)
    precio_por_hora = fields.Float('Sueldo/hora', store=True)
    numero_de_horas =  fields.Integer('Número de horas', store=True)

    total = fields.Monetary(string='Total', store=True, readonly=True, compute='_calcularTotalLine')
    _rec_name = 'apodo'

    @api.one
    @api.depends('numero_de_horas', 'precio_por_hora')
        def _calcularTotalLine(self):
             self.update({
                    'total': (self.precio_por_hora * self.numero_de_horas)
                })

    @api.onchange('employee_id')
        def onchange_employee_id(self):
            addr = {}
            if not self.employee_id.display_name:
                return addr
            if not self.employee_id.apodo:
                self.apodo = "no apodo"
            else:
                self.apodo = self.employee_id.apodo
                self.precio_por_hora = self.employee_id.precio_por_hora
            return addr

这是你的 One2many(我添加参数名称是为了更清楚地说明情况):

# The One2Many relation of the trouble
employee_line = fields.One2many(
    comodel_name='employee.line', 
    inverse_name='id', 
    string='Employee Lines',
    store=True,
)

使用该代码,您告诉 Odoo 在 employee.line 模型中有一个名为 idMany2one 字段指向 employee.as.product 模型.当然,employee.line模型中有一个id字段,但根本不是employee.as.product的ID。它是每个记录的键,在每个模型中默认创建(在本例中为 employee.line),因此您不能将其设置为 One2manyinverse_name .

你要的是这个:

employee.line 模型中创建一个 Many2one 字段:

employee_as_product_id = fields.Many2one(
    comodel_name='employee.as.product',
    string='Empleado/Producto',
)

以这种方式更正 employee.as.product 模型中的 One2many 字段:

employee_line = fields.One2many(
    comodel_name='employee.line', 
    inverse_name='employee_as_product_id', 
    string='Employee Lines',
    store=True,
)

并且 One2many 字段将按预期工作。