تخطي للذهاب إلى المحتوى
القائمة
لقد تم الإبلاغ عن هذا السؤال
1 الرد
8309 أدوات العرض

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
يناير 25
1666
2
سبتمبر 22
9410
2
أبريل 22
4437
0
يوليو 21
7133
1
مارس 21
4826