I have problem with copy one2many field. I create assignment field for Project:
assignment_ids = fields.One2many(
string='Project Assignments',
comodel_name='project.assignment',
inverse_name='project_id',
track_visibility='onchange',)
First I try to add attribute copy=True and then it throws an error
Error while validating constraint
Expected singleton: project.assignment(3, 4)
And this has the same singleton error
@api.multi
def copy(self, default=None):
default = default or {}
for rec in self:
list_assignment = self.assignment_ids
new_assignment = []
for assignment in list_assignment:
new_assignment.append((0, 0, {'id': assignment.id,
'role_id': assignment.role_id.id,
'date_join': date.today(),
'user_id': assignment.user_id.id,
}))
default['assignment_ids'] = new_assignment
return super(ProjectProject, self).copy(default)
Then I try to override the copy function. The assignment have a copy to new Project but old Project's assignment has been deleted.
@api.multi
def copy(self, default=None):
default = default or {}
list_assignment = self.assignment_ids
default['assignment_ids'] = (6, True, list_assignment)
return super(ProjectProject, self).copy(default)
So how can I copy this one2many field?
Thank you so much!!!
This code works when I copied one2many from sale to one2many field in invoice.
def _prepare_invoice(self):
self.ensure_one()
res=return super([Your_Class_Name], self)._prepare_invoice()
# Loop in your One2many field in sale order
one2many_vals =[]
for l in self.order_id.[Your_One2many]:
one2many_vals.append(
(0,0,{'One2many_column1_in_invoice_One2Many':l.[One2many_column1_in_sale_One2Many],
'One2many_column2_in_invoice_One2Many':l.[One2many_column2_in_sale_One2Many],
'One2many_column3_in_invoice_One2Many':l.[One2many_column3_in_sale_One2Many]
}))
res['One2many_field_name_in_invoice'] = one2many_vals
return res