有我的代码,当我选择要更改单位的产品时 测量。我的代码中的错误是什么:
class Product(models.Model):
_name = 'operation.product'
product_id = fields.Many2one('product.product', string="Product")
product_qty = fields.Float(digits=(6, 2), help="Quantity")
product_uom= fields.Many2one('product.uom', string ="Unit of Measure",required=True)
def onchange_product_id(self, cr, uid, ids, product_id, context=None):
""" Changes UoM if product_id changes.
@param product_id: Changed product_id
@return: Dictionary of changed values
"""
res = {}
if product_id:
prod = self.pool.get('product.product').browse(cr, uid, product_id, context=context)
res['value'] = {
'product_uom': prod.uom_id.id
}
return res
operation.xml
<form string="Product">
<sheet>
<group>
<field name="product_id" on_change="onchange_product_id(product_id)"/>
<field name="product_qty" />
</group>
<group>
<field name="product_uom"/>
</group>
</sheet>
</form>
我该如何正确行事?我想有一个最近api8的方法,如下所示:
@api.onchange('amount', 'unit_price')
def _onchange_price(self):
# set auto-changing field
self.price = self.amount * self.unit_price
# Can optionally return a warning and domains
return {
'warning': {`enter code here`
'title': "Something bad happened",
'message': "It was very bad indeed",
}
}
答案 0 :(得分:0)
这里有odoo8 onchange方法的示例:https://www.odoo.com/forum/help-1/question/odoo-8-api-onchange-example-how-to-copy-field-value-to-other-field-74838
你的方法看起来很好除了返回部分,所以只需删除return就可以了。
希望它有所帮助:)
答案 1 :(得分:0)
实际上只有当你想为产品选择其他产品时才需要product_uom,你可以拿相关字段并使其只读,例如
product_uom = fields.Many2one('product.uom', string='Unit of Measure', related='product_id.uom_id', store=True, readonly=True)
如果您将获取相关字段,则无需为uom编写onchange。
以下是代码的onchange方法。
class Product(models.Model):
_name = 'operation.product'
product_id = fields.Many2one('product.product', string="Product")
product_qty = fields.Float(digits=(6, 2), help="Quantity")
product_uom= fields.Many2one('product.uom', string ="Unit of Measure",required=True)
@api.onchange('product_id')
def onchange_product_id(self):
""" Changes UoM if product_id changes.
@param product_id: Changed product_id
@return: Dictionary of changed values
"""
if self.product_id:
self.product_uom = self.product.uom_id