我正在构建一个模块(Odoo 8),我的目标是按销售订单创建优惠,此优惠可以为确定的产品设置固定价格或将礼品设置为零成本。
我在销售订单中的新标签中添加我的自定义模型offer_line。
定义如下:
class OfferSaleOrderLine(models.Model):
_name = 'offer.sale.order.line'
sale_order_ref = fields.Many2one('sale.order',ondelete='set null', string="Sale Order", index=True)
offer_ref = fields.Many2one('offer',ondelete='set null', string="Oferta", index=True)
is_active = fields.Boolean(default=True,string='Activo')
accumulations = fields.Float(digits=(6, 2), help="Acumulaciones")
class SaleOrder(models.Model):
_inherit = 'sale.order'
offers_lines = fields.One2many('offer.sale.order.line','sale_order_ref', string="Lineas de Ofertas")
我在销售订单中有一个新的api onchange方法:
@api.onchange('offers_lines')
def _onchange_offers_lines(self):
我检查是需要申请,我从这个onchange函数添加到offers_line新行,如下所示:
self.offers_lines += self.env['offer.sale.order.line'].new({'is_active': True, 'offer_ref': offer, 'accumulations' : is_offer})
这是完美的工作,创建了行,在表单中添加了tab,触发了onchange方法。
但接下来的问题是,如果我在销售订单行中尝试相同,则无法正常工作:
val = {
'name': gift_line.free_product.name,
'order_id': self.id,
'product_id': gift_line.free_product.id,
'product_uom_qty': gift_line.qty,
'product_uom': self.order_line[0].product_uom.id,
'price_unit': 0.0,
'state': 'draft',
}
self.order_line += self.env['sale.order.line'].new(val)
在日志中,创建了这一行,我可以看到newid id是在我foreach self.order_line
时创建的。**** ORDER LINES:ID :;产品:product.product(5152,);数量:6.0;价格:0.0; ****
但该商品未在销售订单行标签中创建,我不知道为什么,我的自定义行(One2many)已创建,但是,也没有创建具有相同代码和one2many字段的sale_order_lines。如果我尝试将price_unit设置为此sale_order_lines,我也会遇到同样的问题。日志表示已添加更改,但未在表单中更新。在下一次onchange触发器中,更改是消失的。
感谢所有人!
答案 0 :(得分:1)
@api.onchange('Put Your Onchange Field Here')
def _onchange_offers_lines(self):
vals = {
'name': gift_line.free_product.name,
'order_id': self.id,
'product_id': gift_line.free_product.id,
'product_uom_qty': gift_line.qty,
'product_uom': self.order_line[0].product_uom.id,
'price_unit': 0.0,
'state': 'draft'
}
self.order_line = [(0, 0, vals)]
希望它会对你有所帮助。
答案 1 :(得分:0)
Odoo原本不再支持* 2many字段上的更改。
你可以在这里的openerp.models中看到https://github.com/odoo/odoo/blob/9.0/openerp/models.py#L6050
此外还有关于该主题的讨论:https://github.com/odoo/odoo/issues/2693
答案 2 :(得分:0)
我不确定是否正确理解了您的问题,但我看到了两件事。
首先,您需要检查要扩展的基本模块中是否尚未设置要设置 onchange 的字段。如果是这样,您必须通过在字段中将属性设置为 1 来禁用视图中的旧式 onchange (请记住,通过禁用api-v7 onchange在该字段上将不会调用旧的onchange函数,您可能希望在新的onchange函数中调用它。)
第二个问题是,您无法将项目添加到one2many字段,您可能可以将其添加到one2one字段中。您也无法使用var += value
向关系字段添加项目,您必须使用特殊的tupple(如here所述)。