我需要将many2many字段内容复制到invoices_line_tax_id中另一个默认字段的many2many字段
for rep in ex.repartition_line_ids:
obj = self.env['account.analytic.default']
obj_id = obj.search([('product_id','=',rep.product_id.id)])
for o in obj_id:
if o.product_id.id == rep.product_id.id:
a = o.analytic_id.id
new_line1 = (0, 0, {'product_id': rep.product_id.id,
'name': rep.expedition_id.name,
'account_id':rep.product_id.property_account_expense.id,
'account_analytic_id':a,
'invoice_tax_line_id':[(6,0,rep.product_id.supplier_taxes_id.id)],
'quantity': 1,
'price_unit': rep.montant
})
new_lines1.append(new_line1)
答案 0 :(得分:0)
该视图中的Many2many仅处理ID,如果您注意到 您添加一个元素,然后选择一个存在的元素,或者 您创建一个新数据库,我重复您在数据库中创建一个新数据库。 我不知道您想要什么,但我想您正在寻找一个 关系不是多对多关系。
所以您首先需要在数据库中创建记录,然后添加 其ID到命令列表。
这是解决方案,但我真的不建议这样做:
# always put this at the top outside of the loop
# to be executed only one time
object_2 = self.env['model_of_man2many_field']
obj = self.env['account.analytic.default']
for rep in ex.repartition_line_ids:
obj_id = obj.search([('product_id','=',rep.product_id.id)])
a = False # and this to make sure you are safe if you didn't find analytic_id
for o in obj_id:
if o.product_id.id == rep.product_id.id:
a = o.analytic_id and o.analytic_id.id or False
# I think you forget break here
if a:
break
rec = object_2.create({'product_id': rep.product_id.id,
'name': rep.expedition_id.name,
'account_id':rep.product_id.property_account_expense.id,
'account_analytic_id':a,
'invoice_tax_line_id':[(6,0,rep.product_id.supplier_taxes_id.id)],
'quantity': 1,
'price_unit': rep.montant
})
new_lines1.append(rec.id)
# and finaly
return [(6, 0, new_lines1)]
但是我认为您需要的是搜索现有记录,不是吗?因为差 这是即使您单击取消,您每次单击创建按钮也会创建很多记录。