Skip to Content
Menu
This question has been flagged
3171 Views

Hi.

I have two models. One model has One2Many relation of the other. When i save the model. Only saves one register of the One2many relation. I need save n registers.

Here its the code. Im sorry for spanishnames off the variables.

Thanks in advance.

class EmpleadosProductos(models.Model):

 _name = "employee.as.product"

 #the One2Many realtion od the troble
 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
             })


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


Avatar
Discard
Related Posts Replies Views Activity
1
Dec 24
175
0
Nov 24
67
2
Jul 24
712
0
Apr 24
561
3
Mar 24
586