【问题标题】:Odoo 8 @api.onchange function not let update/Created one2many valueOdoo 8 @api.onchange 函数不允许更新/创建 one2many 值
【发布时间】:2016-03-07 09:48:43
【问题描述】:

我正在构建一个模块(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 函数中添加新行到 offer_line,如下所示:

self.offers_lines += self.env['offer.sale.order.line'].new({'is_active': True, 'offer_ref': offer, 'accumulations' : is_offer})

这是完美的工作,创建行,添加到表单中的选项卡和 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)

在日志中,创建了这行,我可以看到在我 foreach self.order_line 时创建了 newid id

****订单行:ID:;产品:product.product(5152,);数量:6.0;价格:0.0 ;****

但是该项目没有在销售订单行选项卡中创建,我不知道为什么,我的自定义行(One2many)已创建,但是,具有相同代码和 one2many 字段的 sale_order_lines 未创建。如果我尝试将 price_unit 设置为此 sale_order_lines,我也会遇到同样的问题。日志说添加了更改,但没有更新形式。在下一个 onchange 触发器中,更改消失。

谢谢大家!

【问题讨论】:

    标签: python openerp odoo-8


    【解决方案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)]
    

    希望对你有所帮助。

    【讨论】:

    • 到目前为止,您的答案只是代码——它会从添加解释中受益
    【解决方案2】:

    Odoo 本身不再支持 *2many 字段的 onchange。

    您可以在此处的 openerp.models 中看到 https://github.com/odoo/odoo/blob/9.0/openerp/models.py#L6050

    此外还有关于该主题的讨论:https://github.com/odoo/odoo/issues/2693

    【讨论】:

    • 谢谢回复,我读了这个,但我不明白为什么要使用我的自定义模型:offers_lines = fields.One2many('offer.sale.order.line','sale_order_ref', string= “Lineas de Ofertas”)。这也是 one2many,但工作正常。
    • 我认为 sale.order 上的 order_line 已经发生了 onchange。也许那个 onchange 阻止了你的新 onchange?
    • 您好,感谢您的回复,也许这是正确的,我使用按钮解决了我的问题(不是自动的,但似乎是唯一的方法)。
    【解决方案3】:

    我不确定是否正确理解了您的问题,但我看到了两件事。

    首先,您需要检查您要设置的字段 onchange 是否尚未在您要扩展的基本模块中设置。如果是这样,您必须通过在字段中将属性设置为 1 来禁用视图中的旧式 onchange(请记住,通过禁用 api-v7 onchange on the field 不会调用旧的 onchange 函数,您可能希望在新的 onchange 函数中调用它)。

    第二个问题是您不能将项目添加到 one2many 字段,您可能可以添加到 many2one。您也不能使用var += value 向关系字段添加项目,您必须使用特殊元组(如here 所述)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-18
      • 2017-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多