跳至內容
選單
此問題已被標幟
1 回覆
8185 瀏覽次數

Hi All, 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 

Expected singleton: project.assignment(3, 4) 

and this has the same error singleton:

    @api.multi
def copy(self, default=None):
default = default or {}
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!!!





頭像
捨棄
作者

Sudhir Arya it work 

Thank you so much!!

But idk why copy=True doesn't work for me.

最佳答案

Hi Tung,

Normally Copy=True should work to copy the One2many field.
Try the following code:

    @api.multi
def copy(self, default=None):
default = default or {}
new_project = super(ProjectProject, self).copy(default)
for assignment in self.assignment_ids:
# Copy o2m and assign new project
assignment.copy(default={'project_id': new_project.id})
return new_project
頭像
捨棄
作者

Sudhir Arya it work

Thank you so much!!

相關帖文 回覆 瀏覽次數 活動
1
1月 25
1539
2
9月 22
9283
2
4月 22
4302
0
7月 21
6968
1
3月 21
4715