コンテンツへスキップ
メニュー
この質問にフラグが付けられました
1 返信
8365 ビュー

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
1712
2
9月 22
9436
2
4月 22
4463
0
7月 21
7150
1
3月 21
4859