Odoo Help


Overriding product_id onchange method

John Doe
on 5/6/16, 4:43 AM 685 views


I experience a problem when overriding onchange method in sale order line. I can edit vals['name'] for example, but when trying to edit price_unit in any way it doesnt change a bit, and somehow it always stays on product price. I dont even know how is it computed. Should I override something more? Or maybe I override wrong method? Please help.

On 5/6/16, 7:40 AM

Hi John,

The price_unit is set by using two onchange methods. One is "product_id_change" and another one is product_uom_change. You must override both methods.

Thanks & Regards

John Doe
On 5/6/16, 7:47 AM

Hi nikesh,

I think I did what you said, but in my case the method looks different:



def product_id_change(self):

if not self.product_id:

return {'domain': {'product_uom': []}}

vals = {}

domain = {'product_uom': [('category_id', '=', self.product_id.uom_id.category_id.id)]}

if not self.product_uom or (self.product_id.uom_id.category_id.id != self.product_uom.category_id.id):

vals['product_uom'] = self.product_id.uom_id

product = self.product_id.with_context(








name = product.name_get()[0][1]

if product.description_sale:

name += '\n' + product.description_sale

vals['name'] = name


if self.order_id.pricelist_id and self.order_id.partner_id:

vals['price_unit'] = self.env['account.tax']._fix_tax_included_price(product.price, product.taxes_id, self.tax_id)


return {'domain': domain}

@John, Here the line vals['price_unit'] = self.env['account.tax']._fix_tax_included_price(product.price, product.taxes_id, self.tax_id) decides the value of price_unit. You can either override method _fix_tax_included_price or product_id_change itself. To override the methodyou must call super.

on 5/6/16, 8:01 AM

@nikesh, Thanks for all the answers. I got one more little problem, when I change the value of the price_unit and I add the product, it doesnt show the new one. The code I changed looks like this: price1 = product.price / product.uos_coeff vals['price_unit'] = self.env['account.tax']._fix_tax_included_price(price1, product.taxes_id, self.tax_id)

John Doe
on 5/6/16, 8:11 AM

@John The price_unit is set by using two onchange methods. One is "product_id_change" and another one is product_uom_change. You must override both methods. @api.onchange('product_uom', 'product_uom_qty') def product_uom_change(self): if not self.product_uom: self.price_unit = 0.0 return if self.order_id.pricelist_id and self.order_id.partner_id: product = self.product_id.with_context( lang=self.order_id.partner_id.lang, partner=self.order_id.partner_id.id, quantity=self.product_uom_qty, date_order=self.order_id.date_order, pricelist=self.order_id.pricelist_id.id, uom=self.product_uom.id, fiscal_position=self.env.context.get('fiscal_position') ) self.price_unit = self.env['account.tax']._fix_tax_included_price(product.price, product.taxes_id, self.tax_id)

on 5/7/16, 12:05 AM

I edited my answer above

on 5/7/16, 12:07 AM

@nikesh Thank you very much, it works perfectly from the odoo side, but I still got problems when creating a sale order from website shop. It still doesnt change the price this way.

John Doe
on 5/9/16, 3:22 AM

Akhil P Sivan

--Akhil P Sivan--
Akhil P Sivan
| 5 3 7
Kochi, India
--Akhil P Sivan--

Interested in exploring Odoo both technically & functionality wise.

email-id : akhilpsivan01@gmail.com

Akhil P Sivan
On 5/6/16, 8:34 AM

Hi John Doe,

Try like this:



def product_id_change(self):

res = super(sale_order_line, self).product_id_change()

for line in self:


             ............. # do your stuff

             line.price_unit = self.env['account.tax']._fix_tax_included_price(product.price, product.taxes_id, self.tax_id)

return res

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)


Asked: 5/6/16, 4:43 AM
Seen: 685 times
Last updated: 5/9/16, 3:25 AM