0

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

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

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

验证约束时出错

预期单例: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 字段呢?

非常感谢!!!

4

1 回答 1

0

当我将 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
于 2021-09-30T15:06:04.073 回答