例如,我有 DICT 数据,里面有几行。我想更新其他模型中的记录行,我使用此代码。其他模型记录有一些行初始化,我想从 DATAS 添加新行。我的代码只是覆盖已经存在的行。如何添加到现有行,而不覆盖?
datas = safe_eval(self.datas)
domain = [('quatation_id', '=', self.id)]
shoes = self.env['shoes.order'].search(domain, limit=1)
for line in datas['lines']:
line = line and line[2]
vals = {
u'product_id': line.get('product_id'),
u'amount_total': line.get('price'),
}
shoes.service_ids.write(vals)
class ShoesOrderService(models.Model):
_name = 'shoes.order.service'
_rec_name = 'product_id'
product_id = fields.Many2one(
'product.product', 'Service',
domain=[('type', '=', 'service')],
)
price = fields.Float()
order_id = fields.Many2one(
'shoes.order', 'Shoes Order')
class ShoesOrder(models.Model):
_name = 'shoes.order'
service_ids = fields.One2many(
'shoes.order.service', 'order_id', string='Additional Services',
copy=True
当你在任何x2many
字段上运行write
时,你会说"这是唯一应该在该字段上的东西。抹去其他任何东西。
您希望添加其他行,因此必须使用此处所述的方法填写字段。
shoes.service_ids = [(4, {ID of record to link})]