This question has been flagged
2 Replies
12534 Views

Hi. I want to copy all the information stored in the one2many relation when I double the entry to the view. All values except for the on2many relation are copied. I have even tried to overwrite the copy function to do it by hand but there is something I must be doing wrong or that I have not come to understand.

Here its the code of the class

class EmpleadosProductos(models.Model):
_name = "employee.as.product"
collection_lines = {}
employee_line = fields.One2many(
'employee.line',
'order_id',
string='Employee Lines'
)
state = fields.Selection([('creado', 'Creado'),
('confirmado', 'Confirmado'),
('cancelado', 'Cancelado'),
('validado', 'Validado'),
('pagado', 'Pagado')
], string='Status', index=True, readonly=True, track_visibility='onchange', copy=False, default='creado', required=True, help='Estado del parte de empleado')
companyias = fields.Many2one('res.partner', 'Obra', 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)
# referencia = fields.Char(string='Ref', required=True)
journal_id = fields.Many2one('account.journal', 'Journal')
_sql_constraints = [
('refererecia_constraint', 'unique(referencia)', 'La referencia debe de ser única!'),
]
@api.multi
def action_confirmar(self):
self.write({'state': 'confirmado'})
@api.multi
def action_cancelar(self):
self.write({'state': 'cancelado'})
@api.multi
def action_validar(self):
self.write({'state': 'validado'})
@api.multi
def action_pagar(self):
self.write({'state': 'pagado'})
@api.multi
def copy(self, default):
default.update({
'employee_line' : self.employee_line,
'referencia': '',
})
return super(EmpleadosProductos, self).copy(default)
Avatar
Discard
Best Answer

Hello Carlos,

add below attribute in One2many line and upgrade your module.

copy=True

Example with your source ==>
employee_line = fields.One2many(
                         'employee.line',
                         'order_id',
string='Employee Lines',
                         copy=True)

Avatar
Discard
Best Answer

Hello Carlos, please try this, If you have any problem, let me know:

@api.multi
def copy(self, default=None):
default = default or {}
ids = self.one2many_ids.ids
    default['one2many_ids'] = [(6, False, ids)]
    return super(ClasName, self).copy(default)
Avatar
Discard