how to use @api.constrains decorated method ?

Hello , in this blog we will learn the definition of @api.constrains in Odoo and it’s use cases.

To enforce date integrity Odoo support tow type of constraints : SQL and Python we are going to talk about the Pythonic way @api.constrains.

In our daily usage of Odoo we face some condition that should be checked before  using theme in our functions ,for example : check start_date is below end_date , make sure the marks do not underpass 20 (which will be our case in this post).



Explanation :

Here we have declared a marks class  and it’s corresponding fields , we want to check that the marks Float exam 1,exam 2,average does not exceed 20.

So we’re going to loop on our record using (which we are going to explain on next posts). then declare @api.constraints containing the fields which we are going to check.  if one or all the fields exceed 20 we trigger an exception and we got a popup we the defined message.

Note : for @api.depends() you can check  the previous post


if you want to try this you can find the code source under our OdooForBeginners github repositories on this link :


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.