簡體   English   中英

為什么我的One2Many字段不保存多個記錄?

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

我有兩個模型。 一個模型有一個One2Many字段指向另一個模型。 當我保存主記錄時,它只保存一個One2many關系的記錄。 我需要它來保存'n'個記錄。 也許問題是我計划的數據結構。

這是代碼。 對於變量的西班牙語名稱,我感到抱歉。

提前致謝。

這是具有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 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字段將按預期工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM