Odoo onchange 无法正常工作
Odoo onchange not working correctly
我继承了 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方法会最先或最后被触发,但是原来product_id
的onchange方法在purchase.order.line
中的变化是设置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
我继承了 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方法会最先或最后被触发,但是原来product_id
的onchange方法在purchase.order.line
中的变化是设置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