简体   繁体   English

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

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

I have two models. 我有两个模型。 One model has an One2Many field pointing to the other one. 一个模型有一个One2Many字段指向另一个模型。 When I save the main record, it only saves one record of the One2many relation. 当我保存主记录时,它只保存一个One2many关系的记录。 I need it to save 'n' records. 我需要它来保存'n'个记录。 Maybe the problem is the structure of the data I planned. 也许问题是我计划的数据结构。

Here is the code. 这是代码。 I'm sorry for the Spanish names of the variables. 对于变量的西班牙语名称,我感到抱歉。

Thanks in advance. 提前致谢。

This is the first class it calls the second class with One2Many relation. 这是具有One2Many关系的第二类。

class EmpleadosProductos(models.Model): _name = "employee.as.product" 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
                })

This is the second class called in the One2Many relation. 这是在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

This is your One2many (I'm adding param names to explain the situation clearer): 这是您的One2many (我正在添加参数名称以更清楚地说明情况):

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

With that code you are telling Odoo that there is a Many2one field named id in employee.line model which points to employee.as.product model. 使用该代码,您将告诉Odoo, employee.line模型中有一个名为idMany2one字段,它指向employee.as.product模型。 Of course, there is an id field in employee.line model, but it is not an ID of employee.as.product at all. 当然, employee.line模型中有一个id字段,但它根本不是employee.as.product的ID。 It is the key of each record, created by default in every model (in this case employee.line ), so you cannot set it as the inverse_name of the One2many . 它是每个记录的键,默认在每个模型中创建(在本例中为employee.line ),因此您不能将其设置为One2manyinverse_name

What you want is this: 您想要的是:

Create a Many2one field in employee.line model: employee.line模型中创建Many2one字段:

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

Correct your One2many field in employee.as.product model this way: 通过以下方式更正employee.as.product模型中的One2many字段:

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

And the One2many field will work as expected. 并且One2many字段将按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM