我继承了purchase.order.line并尝试更改字段中的值。对于product_qty,我可以更改值,但对于price_unit,我无法更改值。
我的自定义 .py 文件:
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')。
有什么解决办法吗?
最佳答案
您无法预测哪个 onchange 方法将首先或最后被触发,但 purchase.order.line
中 product_id
更改的原始 onchange 方法正在设置 price_unit
字段,但不是 product_qty
字段。
看来您的方法是在另一个方法之前调用的,因为 price_unit
被覆盖了。您可以通过调试这两种方法来检查这一点。
现在该怎么办?我更喜欢原始方法的扩展:
@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 事件和字段重新计算。
尝试扩展这两种方法:
@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 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51724911/