Skip to Content
Menu
This question has been flagged
1 Reply
3369 Views

Hi,

I'm tring to update sale order line once Unit Price field changed but constraints does not run.

here my code

class SaleOrderLine(models.Model):
_inherit = 'sale.order.line'
@api.constrains('price_unit')
def _check_price_unit(self):
      for line in self:
    product = line.product_id
standard_price = product.standard_price
if line.price_unit < 250
line.price_unit = min_price

Can you help me about this?

Avatar
Discard
Best Answer

Constraint is needed to forbid applying a certain change. So, you can raise some error or warning, but should not use it to change a wrong value. E.g.:

from odoo.exceptions import UserError

class SaleOrderLine(models.Model):
_inherit = 'sale.order.line'

@api.constrains('price_unit')
def _check_price_unit(self):
for line in self:
product = line.product_id
standard_price = product.standard_price
if line.price_unit < 250
raise UserError(_("The price migth be not less than 250"))

If you want to apply changes based on entered values, you should either use inverse or re-write CRUD methods write / create.

Avatar
Discard