为什么我的One2Many字段不能保存多条记录?

时间:2017-12-13 13:37:45

标签: python python-2.7 odoo-8 odoo-10 odoo

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

这是代码。我很抱歉这些变量的西班牙名字。

提前致谢。

这是第一个用One2Many关系调用第二个类的类。

类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 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

1 个答案:

答案 0 :(得分:0)

这是你的 One2many (我正在添加param名称来解释情况更清晰):

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

使用该代码,您告诉Odoo id模型中有一个名为employee.line Many2one 字段,该字段指向employee.as.product模型。当然,id模型中有一个employee.line字段,但它根本不是employee.as.product的ID。它是默认情况下在每个模型中创建的每条记录的关键(在本例中为employee.line),因此您无法将其设置为 One2many inverse_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字段将按预期工作。