代码之家  ›  专栏  ›  技术社区  ›  Chaban33

添加行而不覆盖现有行

  •  1
  • Chaban33  · 技术社区  · 7 年前

    例如,我有DICT数据,其中有一些行。我想更新其他模型中的记录行,并使用此代码。其他模型记录有一些行init,我想从数据中添加新行。我的代码只是覆盖了已经存在的行。如何添加到现有行,而不覆盖?

     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
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   travisw    7 年前

    当你跑步时 write 在任何 x2many field,你是说“这是该字段上唯一应该出现的内容。删除任何其他内容。”

    the method described here .

    shoes.service_ids = [(4, {ID of record to link})]