There is my code, when I choose a product I want to change the Unit of Measure. what is the mistakes in my code below :
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>
How I have to do correctly? I want to have a method with recently api8 like this:
@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",
}
}
Here you have example of odoo8 onchange method: https://www.odoo.com/forum/help-1/question/odoo-8-api-onchange-example-how-to-copy-field-value-to-other-field-74838
Your method look fine except return part, so just remove return and that should do it.
Hope it helped :)