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

 

and i want to appear message when price_subtotal > minimum_amount 


-----add the field in Product 

  from odoo import api, fields, models, _
  class ProductTemplate(models.Model):  

  _inherit = "product.template"
    minimum_amount = fields.Float('Minimum Order Amount')


-------and i want to appear message when price_subtotal > minimum_amount


class SaleOrderLine(models.Model):  

 _inherit = 'sale.order.line'
     @api.model  

  def create(self,vals):  

      result = super(SaleOrderLine,self).create(vals)    

        if vals.get('product_id'):     

       product = self.env['product.product'].search([('id','=',vals['product_id'])])     

       total_all = vals['price_subtotal']   

         if total_all

           raise ValidationError(_( "Minimum order amount of the product %s is %s.") % (vals['name'],product.minimum_amount))          

          return result


Avatar
Discard
Best Answer

Hi,

Constraints are the rules specified on a record that prevent incorrect data before saving the record. Python constraints are used for the validation process. The validation function will be triggered whenever the validating fields are modified, and then it should raise an exception. Usually, the ‘ValidationError’ is used to show the exception
  from odoo import api, fields, models, _
  class ProductTemplate(models.Model):  

  _inherit = "product.template"
    minimum_amount = fields.Float('Minimum Order Amount')
class SaleOrderLine(models.Model):  

 _inherit = 'sale.order.line'
     @api.constrains('price_subtotal')def _check_price_subtotal(self):
    for record in self:
        if record.price_subtotal > record.product_id.product_tmpl_id.minimum_amount:
            raise ValidationError(_( "Minimum order amount of the product ")
For more details, refer to the book:

https://www.cybrosys.com/odoo/odoo-books/odoo-16-development/creating-odoo-modules/constraints/

Hope it helps

Avatar
Discard