Odoo onchange无法正常工作 [英] Odoo onchange not working correctly

查看:525
本文介绍了Odoo onchange无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我继承了purchase.order.line,并尝试在字段中更改值.对于product_qty,我可以更改值,但是对于price_unit,我不能更改值.

I'm inherit purchase.order.line and try change value in field. For product_qty I can change value but for price_unit I can't change value.

我的自定义.py文件:

My custom .py file:

class PurchaseOrderLine(models.Model):

_inherit = 'purchase.order.line'


@api.onchange('product_id')
def my_fucn(self):
    for rec in self:
        rec.product_qty = 10  #WORKING
        rec.price_unit = 1    #NOT WORKING

也许是问题所在,因为原始的purcahase.py odoo文件中也有@ api.onchange('product_id').

Maybe is problem because in original purcahase.py odoo file also have @api.onchange('product_id').

有解决方案吗?

推荐答案

您无法预测哪个onchange方法将首先或最后被触发,但是purchase.order.lineproduct_id更改的原始onchange方法设置为price_unit字段,但不包含product_qty字段.

You can't predict which onchange method will be triggered first or last, but the original onchange method for product_id changes in purchase.order.line is setting the price_unit field, but not the product_qty field.

因此,似乎您的方法在另一个方法之前被调用,因为price_unit被覆盖了.您可以通过调试这两种方法进行检查.

So it seems your method is called before the other one, because price_unit is overwritten. You can check that by debugging both methods.

现在该怎么办?我希望扩展原始方法:

What to do now? I would prefer the extension of the original method:

@api.onchange('product_id')
def the_original_method(self):
    res = super(PurchaseOrderLine, self).the_original_method()
    # your logic here
    return res

在您的情况下,product_qty更改将触发另一个onchange事件.请始终记住,字段更改会触发onchange事件和字段重新计算.

In your case a product_qty change will trigger another onchange event. Always have in mind, that field changes can trigger onchange events and field recomputations.

尝试扩展两种方法:

@api.onchange('product_id')
def onchange_product_id(self):
    res = super(PurchaseOrderLine, self).onchange_product_id()
    # your logic here
    for rec in self:
        rec.product_qty = 10  # will trigger _onchange_quantity() on return

    return res

@api.onchange('product_qty', 'product_uom')
def _onchange_quantity(self):
    res = super(PurchaseOrderLine, self)._onchange_quantity()
    # your logic here
    for rec in self:
        rec.price_unit = 1.0

    return res

这篇关于Odoo onchange无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆