SlideShare a Scribd company logo
5
Most read
How to Use
api.constrains() in
Odoo 17
Enterprise
Introduction
Enterprise
In this slide, we will discuss the api.constrains() decorator in
Odoo 17, a powerful tool for defining custom data validation
rules within your models. We will explore its implementation,
usage scenarios, and essential considerations for effective
data integrity in your Odoo applications.
Enterprise
What is api.constrains()
● A decorator used to define custom data validation rules
in Odoo models.
● Ensures data adheres to specific business logic before
being saved to the database.
● Offers greater flexibility compared to standard database
constraints.
Enterprise
Using api.constrains()
● Define a method decorated with @api.constrains(*fields).
● The *fields argument specifies the fields involved in the
validation.
● The method should raise a ValidationError if the
validation fails.
Enterprise
Example: Validating product price
from odoo.exceptions import ValidationError
class Product(models.Model):
_name = 'product.product'
name = fields.Char(string='Product Name', required=True)
list_price = fields.Float(string='List Price', required=True)
cost_price = fields.Float(string='Cost Price', required=True)
@api.constrains('list_price', 'cost_price')
def _check_price(self):
if self.list_price < self.cost_price:
raise ValidationError("List price cannot be lower
than cost price!")
Enterprise
This example demonstrates how to validate the list price of a
product against its cost price. The _check_price method,
decorated with @api.constrains('list_price', 'cost_price'),
ensures that the list price is always greater than or equal to the
cost price. If this condition is violated, a ValidationError is
raised, preventing the product from being saved with an
invalid price configuration.
For More Info.
Check our company website for related
blogs and Odoo book.
Check our YouTube channel for
functional and technical videos in Odoo.
Enterprise
www.cybrosys.com
Ad

Recommended

PPTX
How to Setup Automatic Reordering Rule in Odoo 18 Inventory
Celine George
 
PPTX
How to Add a Custom Button in Odoo 18 POS Screen
Celine George
 
PPTX
How to Create & Manage Stages in Odoo 18 Helpdesk
Celine George
 
PPTX
How to Configure Refusal of Applicants in Odoo 18 Recruitment
Celine George
 
PPTX
How to Manage Wins & Losses in Odoo 18 CRM
Celine George
 
PPTX
How to Configure Taxes in Company Currency in Odoo 18 Accounting
Celine George
 
PPTX
How to use _name_search() method in Odoo 18
Celine George
 
PPTX
How to use grouped() method in Odoo 18 - Odoo Slides
Celine George
 
PPTX
How to use ensure_one() method in Odoo 18
Celine George
 
PPTX
How to Add New Item in CogMenu in Odoo 18
Celine George
 
PPTX
How to use search fetch method in Odoo 18
Celine George
 
PPTX
List View Components in Odoo 18 - Odoo Slides
Celine George
 
PPTX
How payment terms are configured in Odoo 18
Celine George
 
PPTX
How to Customize Quotation Layouts in Odoo 18
Celine George
 
PPTX
Code Profiling in Odoo 18 - Odoo 18 Slides
Celine George
 
PPTX
How to Manage Different Customer Addresses in Odoo 18 Accounting
Celine George
 
PPTX
How to Implement Least Package Removal Strategy in Odoo 18 Inventory
Celine George
 
PPTX
How to Manage Inventory Movement in Odoo 18 POS
Celine George
 
PPTX
How to Manage Multi Language for Invoice in Odoo 18
Celine George
 
PPTX
Overview of Off Boarding in Odoo 18 Employees
Celine George
 
PPTX
Overview of Employee in Odoo 18 - Odoo Slides
Celine George
 
PPTX
How to Manage & Create a New Department in Odoo 18 Employee
Celine George
 
PPTX
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
PPTX
How to Configure Vendor Management in Lunch App of Odoo 18
Celine George
 
PPTX
How to Manage Upselling of Subscriptions in Odoo 18
Celine George
 
PPTX
How to Create Quotation Templates Sequence in Odoo 18 Sales
Celine George
 
PPTX
How to Create a Rainbow Man Effect in Odoo 18
Celine George
 
PPTX
How to Manage Maintenance Request in Odoo 18
Celine George
 
PPTX
Pests of Maize: An comprehensive overview.pptx
Arshad Shaikh
 
PPTX
F-BLOCK ELEMENTS POWER POINT PRESENTATIONS
mprpgcwa2024
 

More Related Content

More from Celine George (20)

PPTX
How to use ensure_one() method in Odoo 18
Celine George
 
PPTX
How to Add New Item in CogMenu in Odoo 18
Celine George
 
PPTX
How to use search fetch method in Odoo 18
Celine George
 
PPTX
List View Components in Odoo 18 - Odoo Slides
Celine George
 
PPTX
How payment terms are configured in Odoo 18
Celine George
 
PPTX
How to Customize Quotation Layouts in Odoo 18
Celine George
 
PPTX
Code Profiling in Odoo 18 - Odoo 18 Slides
Celine George
 
PPTX
How to Manage Different Customer Addresses in Odoo 18 Accounting
Celine George
 
PPTX
How to Implement Least Package Removal Strategy in Odoo 18 Inventory
Celine George
 
PPTX
How to Manage Inventory Movement in Odoo 18 POS
Celine George
 
PPTX
How to Manage Multi Language for Invoice in Odoo 18
Celine George
 
PPTX
Overview of Off Boarding in Odoo 18 Employees
Celine George
 
PPTX
Overview of Employee in Odoo 18 - Odoo Slides
Celine George
 
PPTX
How to Manage & Create a New Department in Odoo 18 Employee
Celine George
 
PPTX
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
PPTX
How to Configure Vendor Management in Lunch App of Odoo 18
Celine George
 
PPTX
How to Manage Upselling of Subscriptions in Odoo 18
Celine George
 
PPTX
How to Create Quotation Templates Sequence in Odoo 18 Sales
Celine George
 
PPTX
How to Create a Rainbow Man Effect in Odoo 18
Celine George
 
PPTX
How to Manage Maintenance Request in Odoo 18
Celine George
 
How to use ensure_one() method in Odoo 18
Celine George
 
How to Add New Item in CogMenu in Odoo 18
Celine George
 
How to use search fetch method in Odoo 18
Celine George
 
List View Components in Odoo 18 - Odoo Slides
Celine George
 
How payment terms are configured in Odoo 18
Celine George
 
How to Customize Quotation Layouts in Odoo 18
Celine George
 
Code Profiling in Odoo 18 - Odoo 18 Slides
Celine George
 
How to Manage Different Customer Addresses in Odoo 18 Accounting
Celine George
 
How to Implement Least Package Removal Strategy in Odoo 18 Inventory
Celine George
 
How to Manage Inventory Movement in Odoo 18 POS
Celine George
 
How to Manage Multi Language for Invoice in Odoo 18
Celine George
 
Overview of Off Boarding in Odoo 18 Employees
Celine George
 
Overview of Employee in Odoo 18 - Odoo Slides
Celine George
 
How to Manage & Create a New Department in Odoo 18 Employee
Celine George
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
How to Configure Vendor Management in Lunch App of Odoo 18
Celine George
 
How to Manage Upselling of Subscriptions in Odoo 18
Celine George
 
How to Create Quotation Templates Sequence in Odoo 18 Sales
Celine George
 
How to Create a Rainbow Man Effect in Odoo 18
Celine George
 
How to Manage Maintenance Request in Odoo 18
Celine George
 

Recently uploaded (20)

PPTX
Pests of Maize: An comprehensive overview.pptx
Arshad Shaikh
 
PPTX
F-BLOCK ELEMENTS POWER POINT PRESENTATIONS
mprpgcwa2024
 
PPTX
SCHIZOPHRENIA OTHER PSYCHOTIC DISORDER LIKE Persistent delusion/Capgras syndr...
parmarjuli1412
 
PPTX
NSUMD_M1 Library Orientation_June 11, 2025.pptx
Julie Sarpy
 
PPTX
ENGLISH-5 Q1 Lesson 1.pptx - Story Elements
Mayvel Nadal
 
PPTX
Photo chemistry Power Point Presentation
mprpgcwa2024
 
PPTX
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
Ronisha Das
 
PDF
LDMMIA Yoga S10 Free Workshop Grad Level
LDM & Mia eStudios
 
PPTX
CRYPTO TRADING COURSE BY FINANCEWORLD.IO
AndrewBorisenko3
 
PDF
HistoPathology Ppt. Arshita Gupta for Diploma
arshitagupta674
 
PPTX
Great Governors' Send-Off Quiz 2025 Prelims IIT KGP
IIT Kharagpur Quiz Club
 
PPTX
June 2025 Progress Update With Board Call_In process.pptx
International Society of Service Innovation Professionals
 
PDF
K12 Tableau User Group virtual event June 18, 2025
dogden2
 
PDF
University of Ghana Cracks Down on Misconduct: Over 100 Students Sanctioned
Kweku Zurek
 
PDF
Romanticism in Love and Sacrifice An Analysis of Oscar Wilde’s The Nightingal...
KaryanaTantri21
 
PPTX
Q1_TLE 8_Week 1- Day 1 tools and equipment
clairenotado3
 
PDF
This is why students from these 44 institutions have not received National Se...
Kweku Zurek
 
PPT
M&A5 Q1 1 differentiate evolving early Philippine conventional and contempora...
ErlizaRosete
 
PPTX
OBSESSIVE COMPULSIVE DISORDER.pptx IN 5TH SEMESTER B.SC NURSING, 2ND YEAR GNM...
parmarjuli1412
 
PDF
Public Health For The 21st Century 1st Edition Judy Orme Jane Powell
trjnesjnqg7801
 
Pests of Maize: An comprehensive overview.pptx
Arshad Shaikh
 
F-BLOCK ELEMENTS POWER POINT PRESENTATIONS
mprpgcwa2024
 
SCHIZOPHRENIA OTHER PSYCHOTIC DISORDER LIKE Persistent delusion/Capgras syndr...
parmarjuli1412
 
NSUMD_M1 Library Orientation_June 11, 2025.pptx
Julie Sarpy
 
ENGLISH-5 Q1 Lesson 1.pptx - Story Elements
Mayvel Nadal
 
Photo chemistry Power Point Presentation
mprpgcwa2024
 
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
Ronisha Das
 
LDMMIA Yoga S10 Free Workshop Grad Level
LDM & Mia eStudios
 
CRYPTO TRADING COURSE BY FINANCEWORLD.IO
AndrewBorisenko3
 
HistoPathology Ppt. Arshita Gupta for Diploma
arshitagupta674
 
Great Governors' Send-Off Quiz 2025 Prelims IIT KGP
IIT Kharagpur Quiz Club
 
June 2025 Progress Update With Board Call_In process.pptx
International Society of Service Innovation Professionals
 
K12 Tableau User Group virtual event June 18, 2025
dogden2
 
University of Ghana Cracks Down on Misconduct: Over 100 Students Sanctioned
Kweku Zurek
 
Romanticism in Love and Sacrifice An Analysis of Oscar Wilde’s The Nightingal...
KaryanaTantri21
 
Q1_TLE 8_Week 1- Day 1 tools and equipment
clairenotado3
 
This is why students from these 44 institutions have not received National Se...
Kweku Zurek
 
M&A5 Q1 1 differentiate evolving early Philippine conventional and contempora...
ErlizaRosete
 
OBSESSIVE COMPULSIVE DISORDER.pptx IN 5TH SEMESTER B.SC NURSING, 2ND YEAR GNM...
parmarjuli1412
 
Public Health For The 21st Century 1st Edition Judy Orme Jane Powell
trjnesjnqg7801
 
Ad

How to Use api.constrains ( ) in Odoo 17

  • 1. How to Use api.constrains() in Odoo 17 Enterprise
  • 2. Introduction Enterprise In this slide, we will discuss the api.constrains() decorator in Odoo 17, a powerful tool for defining custom data validation rules within your models. We will explore its implementation, usage scenarios, and essential considerations for effective data integrity in your Odoo applications.
  • 3. Enterprise What is api.constrains() ● A decorator used to define custom data validation rules in Odoo models. ● Ensures data adheres to specific business logic before being saved to the database. ● Offers greater flexibility compared to standard database constraints.
  • 4. Enterprise Using api.constrains() ● Define a method decorated with @api.constrains(*fields). ● The *fields argument specifies the fields involved in the validation. ● The method should raise a ValidationError if the validation fails.
  • 5. Enterprise Example: Validating product price from odoo.exceptions import ValidationError class Product(models.Model): _name = 'product.product' name = fields.Char(string='Product Name', required=True) list_price = fields.Float(string='List Price', required=True) cost_price = fields.Float(string='Cost Price', required=True) @api.constrains('list_price', 'cost_price') def _check_price(self): if self.list_price < self.cost_price: raise ValidationError("List price cannot be lower than cost price!")
  • 6. Enterprise This example demonstrates how to validate the list price of a product against its cost price. The _check_price method, decorated with @api.constrains('list_price', 'cost_price'), ensures that the list price is always greater than or equal to the cost price. If this condition is violated, a ValidationError is raised, preventing the product from being saved with an invalid price configuration.
  • 7. For More Info. Check our company website for related blogs and Odoo book. Check our YouTube channel for functional and technical videos in Odoo. Enterprise www.cybrosys.com