如何在odoo中复制one2many字段?

How to copy one2many filed in odoo?

我在复制 one2many 字段时遇到问题。我为项目创建分配字段:

        assignment_ids = fields.One2many(
        string='Project Assignments',
        comodel_name='project.assignment',
        inverse_name='project_id',
        track_visibility='onchange',)

首先我尝试添加属性 copy=True 然后它抛出一个错误

Error while validating constraint

Expected singleton: project.assignment(3, 4)

这也有同样的单例错误

@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)

然后我尝试覆盖复制功能。该作业有新项目的副本,但旧项目的作业已被删除。

@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)

那么如何复制这个 one2many 字段?

非常感谢!!!

当我将 one2many 从销售复制到发票中的 one2many 字段时,此代码有效。

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