{"id":14,"date":"2023-07-26T05:05:00","date_gmt":"2023-07-26T05:05:00","guid":{"rendered":""},"modified":"2024-01-28T09:05:19","modified_gmt":"2024-01-28T09:05:19","slug":"contraints-in-odoo","status":"publish","type":"post","link":"https:\/\/pythonpower.in\/index.php\/2023\/07\/26\/contraints-in-odoo\/","title":{"rendered":"contraints in odoo"},"content":{"rendered":"<\/p>\n<div style=\"clear: both; text-align: center;\"><a href=\"http:\/\/pythonpower.in\/wp-content\/uploads\/2023\/07\/Expert-20In-20Odoo-2016-20-20Hindi-2.jpg\" style=\"margin-left: 1em; margin-right: 1em;\"><img loading=\"lazy\" decoding=\"async\" border=\"0\" data-original-height=\"720\" data-original-width=\"1280\" height=\"360\" src=\"http:\/\/pythonpower.in\/wp-content\/uploads\/2023\/07\/Expert-20In-20Odoo-2016-20-20Hindi-2-300x169.jpg\" width=\"640\" \/><\/a><\/div>\n<p>&nbsp;<\/p>\n<p style=\"font-family: &quot;Helvetica Neue&quot;; font-feature-settings: normal; font-kerning: auto; font-optical-sizing: auto; font-stretch: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-variation-settings: normal; line-height: normal; margin: 0px;\"><span style=\"font-size: large;\"><b>Defining Constraints:<\/b><\/span><\/p>\n<p style=\"font-family: &quot;Helvetica Neue&quot;; font-feature-settings: normal; font-kerning: auto; font-optical-sizing: auto; font-stretch: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-variation-settings: normal; line-height: normal; margin: 0px;\"><span style=\"font-size: medium;\">Constraints are defined in the Python code of Odoo models. Each model can have its constraints specified in the _constraints attribute, which is a list of tuples. Each tuple in the list represents a constraint and consists of the following elements:<\/span><\/p>\n<ul>\n<li style=\"font-family: &quot;Helvetica Neue&quot;; font-feature-settings: normal; font-kerning: auto; font-optical-sizing: auto; font-stretch: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-variation-settings: normal; line-height: normal; margin: 0px;\"><span style=\"font-size: medium;\"><span style=\"font-family: Menlo; font-feature-settings: normal; font-kerning: auto; font-optical-sizing: auto; font-stretch: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-variation-settings: normal; line-height: normal;\"><\/span>Name: A unique identifier for the constraint.<\/span><\/li>\n<li style=\"font-family: &quot;Helvetica Neue&quot;; font-feature-settings: normal; font-kerning: auto; font-optical-sizing: auto; font-stretch: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-variation-settings: normal; line-height: normal; margin: 0px;\"><span style=\"font-size: medium;\"><span style=\"font-family: Menlo; font-feature-settings: normal; font-kerning: auto; font-optical-sizing: auto; font-stretch: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-variation-settings: normal; line-height: normal;\"><\/span>Method: The name of the Python method that implements the constraint logic.<\/span><\/li>\n<li style=\"font-family: &quot;Helvetica Neue&quot;; font-feature-settings: normal; font-kerning: auto; font-optical-sizing: auto; font-stretch: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-variation-settings: normal; line-height: normal; margin: 0px;\"><span style=\"font-size: medium;\"><span style=\"font-family: Menlo; font-feature-settings: normal; font-kerning: auto; font-optical-sizing: auto; font-stretch: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-variation-settings: normal; line-height: normal;\"><\/span>Message: An optional error message to display when the constraint is violated.<\/span><\/li>\n<\/ul>\n<p style=\"font-family: &quot;Helvetica Neue&quot;; font-feature-settings: normal; font-kerning: auto; font-optical-sizing: auto; font-stretch: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-variation-settings: normal; line-height: normal; margin: 0px;\"><span style=\"font-size: medium;\">Here&#8217;s an example of defining a simple constraint on the name field of a model:<\/span><\/p>\n<p style=\"font-family: &quot;Helvetica Neue&quot;; font-feature-settings: normal; font-kerning: auto; font-optical-sizing: auto; font-stretch: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-variation-settings: normal; line-height: normal; margin: 0px;\"><span style=\"font-size: medium;\"><br \/><\/span><\/p>\n<p style=\"font-feature-settings: normal; font-kerning: auto; font-optical-sizing: auto; font-stretch: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-variation-settings: normal; line-height: normal; margin: 0px;\"><span style=\"font-family: Helvetica Neue; font-size: medium;\">from odoo import models, fields, api<\/span><\/p>\n<p style=\"font-feature-settings: normal; font-kerning: auto; font-optical-sizing: auto; font-stretch: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-variation-settings: normal; line-height: normal; margin: 0px;\"><span style=\"font-family: Helvetica Neue; font-size: medium;\"><br \/><\/span><\/p>\n<p style=\"font-feature-settings: normal; font-kerning: auto; font-optical-sizing: auto; font-stretch: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-variation-settings: normal; line-height: normal; margin: 0px;\"><span style=\"font-family: Helvetica Neue; font-size: medium;\">class MyModel(models.Model):<\/span><\/p>\n<p style=\"font-feature-settings: normal; font-kerning: auto; font-optical-sizing: auto; font-stretch: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-variation-settings: normal; line-height: normal; margin: 0px;\"><span style=\"font-family: Helvetica Neue; font-size: medium;\">&nbsp; &nbsp; _name = &#8216;my.model&#8217;<\/span><\/p>\n<p style=\"font-feature-settings: normal; font-kerning: auto; font-optical-sizing: auto; font-stretch: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-variation-settings: normal; line-height: normal; margin: 0px;\"><span style=\"font-family: Helvetica Neue; font-size: medium;\">&nbsp; &nbsp;&nbsp;<\/span><\/p>\n<p style=\"font-feature-settings: normal; font-kerning: auto; font-optical-sizing: auto; font-stretch: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-variation-settings: normal; line-height: normal; margin: 0px;\"><span style=\"font-family: Helvetica Neue; font-size: medium;\">&nbsp; &nbsp; name = fields.Char(string=&#8217;Name&#8217;, required=True)<\/span><\/p>\n<p style=\"font-feature-settings: normal; font-kerning: auto; font-optical-sizing: auto; font-stretch: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-variation-settings: normal; line-height: normal; margin: 0px;\"><span style=\"font-family: Helvetica Neue; font-size: medium;\"><br \/><\/span><\/p>\n<p style=\"font-feature-settings: normal; font-kerning: auto; font-optical-sizing: auto; font-stretch: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-variation-settings: normal; line-height: normal; margin: 0px;\"><span style=\"font-family: Helvetica Neue; font-size: medium;\">&nbsp; &nbsp; _constraints = [<\/span><\/p>\n<p style=\"font-feature-settings: normal; font-kerning: auto; font-optical-sizing: auto; font-stretch: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-variation-settings: normal; line-height: normal; margin: 0px;\"><span style=\"font-family: Helvetica Neue; font-size: medium;\">&nbsp; &nbsp; &nbsp; &nbsp; (&#8216;check_name_unique&#8217;, &#8216;unique_name_constraint&#8217;, &#8216;Name must be unique.&#8217;),<\/span><\/p>\n<p style=\"font-feature-settings: normal; font-kerning: auto; font-optical-sizing: auto; font-stretch: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-variation-settings: normal; line-height: normal; margin: 0px;\"><span style=\"font-family: Helvetica Neue; font-size: medium;\">&nbsp; &nbsp; ]<\/span><\/p>\n<p style=\"font-feature-settings: normal; font-kerning: auto; font-optical-sizing: auto; font-stretch: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-variation-settings: normal; line-height: normal; margin: 0px;\"><span style=\"font-family: Helvetica Neue; font-size: medium;\"><br \/><\/span><\/p>\n<p style=\"font-feature-settings: normal; font-kerning: auto; font-optical-sizing: auto; font-stretch: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-variation-settings: normal; line-height: normal; margin: 0px;\"><span style=\"font-family: Helvetica Neue; font-size: medium;\">&nbsp; &nbsp; @api.constrains(&#8216;name&#8217;)<\/span><\/p>\n<p style=\"font-feature-settings: normal; font-kerning: auto; font-optical-sizing: auto; font-stretch: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-variation-settings: normal; line-height: normal; margin: 0px;\"><span style=\"font-family: Helvetica Neue; font-size: medium;\">&nbsp; &nbsp; def unique_name_constraint(self):<\/span><\/p>\n<p style=\"font-feature-settings: normal; font-kerning: auto; font-optical-sizing: auto; font-stretch: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-variation-settings: normal; line-height: normal; margin: 0px;\"><span style=\"font-family: Helvetica Neue; font-size: medium;\">&nbsp; &nbsp; &nbsp; &nbsp; # Custom constraint logic to check for uniqueness<\/span><\/p>\n<p style=\"font-feature-settings: normal; font-kerning: auto; font-optical-sizing: auto; font-stretch: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-variation-settings: normal; line-height: normal; margin: 0px;\"><span style=\"font-family: Helvetica Neue; font-size: medium;\">&nbsp; &nbsp; &nbsp; &nbsp; if self.env[&#8216;my.model&#8217;].search_count([(&#8216;name&#8217;, &#8216;=&#8217;, self.name)]) &gt; 1:<\/span><\/p>\n<p style=\"font-feature-settings: normal; font-kerning: auto; font-optical-sizing: auto; font-stretch: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-variation-settings: normal; line-height: normal; margin: 0px;\">\n<p style=\"font-feature-settings: normal; font-kerning: auto; font-optical-sizing: auto; font-stretch: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-variation-settings: normal; line-height: normal; margin: 0px;\"><span style=\"font-family: Helvetica Neue; font-size: medium;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; raise models.ValidationError(&#8220;The name must be unique.&#8221;)<\/span><\/p>\n<p style=\"font-feature-settings: normal; font-kerning: auto; font-optical-sizing: auto; font-stretch: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-variation-settings: normal; line-height: normal; margin: 0px;\"><span style=\"font-family: Helvetica Neue; font-size: medium;\"><br \/><\/span><\/p>\n<p style=\"font-feature-settings: normal; font-kerning: auto; font-optical-sizing: auto; font-stretch: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-variation-settings: normal; line-height: normal; margin: 0px;\"><span style=\"font-family: Helvetica Neue; font-size: medium;\"><br \/><\/span><\/p>\n<p style=\"font-family: &quot;Helvetica Neue&quot;; font-feature-settings: normal; font-kerning: auto; font-optical-sizing: auto; font-stretch: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-variation-settings: normal; line-height: normal; margin: 0px;\"><span style=\"font-size: large;\">I<b>mplementing the Constraint Logic:<\/b><\/span><\/p>\n<p style=\"font-family: &quot;Helvetica Neue&quot;; font-feature-settings: normal; font-kerning: auto; font-optical-sizing: auto; font-stretch: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-variation-settings: normal; line-height: normal; margin: 0px;\"><span style=\"font-size: medium;\">In the example above, the constraint unique_name_constraint checks whether the name field is unique across the records of the my.model. If it finds duplicate names, it raises a validation error, preventing the creation or modification of the record.<\/span><\/p>\n<p style=\"font-family: &quot;Helvetica Neue&quot;; font-feature-settings: normal; font-kerning: auto; font-optical-sizing: auto; font-stretch: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-variation-settings: normal; line-height: normal; margin: 0px;\"><span style=\"font-size: medium;\"><br \/><\/span><\/p>\n<p style=\"font-family: &quot;Helvetica Neue&quot;; font-feature-settings: normal; font-kerning: auto; font-optical-sizing: auto; font-stretch: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-variation-settings: normal; line-height: normal; margin: 0px;\"><span style=\"font-size: large;\"><b>Raising Validation Errors:<\/b><\/span><\/p>\n<p style=\"font-family: &quot;Helvetica Neue&quot;; font-feature-settings: normal; font-kerning: auto; font-optical-sizing: auto; font-stretch: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-variation-settings: normal; line-height: normal; margin: 0px;\"><span style=\"font-size: medium;\">To enforce the constraint, you need to raise a ValidationError when the condition is not met. This will prevent the operation from being saved to the database.<\/span><\/p>\n<p style=\"font-family: &quot;Helvetica Neue&quot;; font-feature-settings: normal; font-kerning: auto; font-optical-sizing: auto; font-stretch: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-variation-settings: normal; line-height: normal; margin: 0px;\"><span style=\"font-size: medium;\"><br \/><\/span><\/p>\n<p style=\"font-family: &quot;Helvetica Neue&quot;; font-feature-settings: normal; font-kerning: auto; font-optical-sizing: auto; font-stretch: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-variation-settings: normal; line-height: normal; margin: 0px;\"><span style=\"font-size: large;\"><b>Constraint Types:<\/b><\/span><\/p>\n<p style=\"font-family: &quot;Helvetica Neue&quot;; font-feature-settings: normal; font-kerning: auto; font-optical-sizing: auto; font-stretch: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-variation-settings: normal; line-height: normal; margin: 0px;\"><span style=\"font-size: medium;\">In Odoo, you can define two types of constraints: SQL constraints and Python constraints.<\/span><\/p>\n<ul>\n<li style=\"font-family: &quot;Helvetica Neue&quot;; font-feature-settings: normal; font-kerning: auto; font-optical-sizing: auto; font-stretch: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-variation-settings: normal; line-height: normal; margin: 0px;\"><span style=\"font-size: medium;\"><span style=\"font-family: Menlo; font-feature-settings: normal; font-kerning: auto; font-optical-sizing: auto; font-stretch: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-variation-settings: normal; line-height: normal;\"><\/span><b>SQL Constraints<\/b>: These are implemented directly in the database schema and are enforced at the database level. They are more efficient but have some limitations in complex cases.<\/span><\/li>\n<li style=\"font-family: &quot;Helvetica Neue&quot;; font-feature-settings: normal; font-kerning: auto; font-optical-sizing: auto; font-stretch: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-variation-settings: normal; line-height: normal; margin: 0px;\"><span style=\"font-size: medium;\"><span style=\"font-family: Menlo; font-feature-settings: normal; font-kerning: auto; font-optical-sizing: auto; font-stretch: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-variation-settings: normal; line-height: normal;\"><\/span><b>Python Constraints<\/b>: These are implemented as Python methods (like the example above) and are enforced at the application level. Python constraints are more flexible and allow for complex validation logic.<\/span><\/li>\n<\/ul>\n<div><span style=\"font-family: Helvetica Neue; font-size: medium;\"><br \/><\/span><\/div>\n<div>\n<p style=\"font-family: &quot;Helvetica Neue&quot;; font-feature-settings: normal; font-kerning: auto; font-optical-sizing: auto; font-stretch: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-variation-settings: normal; line-height: normal; margin: 0px;\"><span style=\"font-size: large;\"><b>Relational Constraints:<\/b><\/span><\/p>\n<p style=\"font-family: &quot;Helvetica Neue&quot;; font-feature-settings: normal; font-kerning: auto; font-optical-sizing: auto; font-stretch: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-variation-settings: normal; line-height: normal; margin: 0px;\"><span style=\"font-size: medium;\">Apart from constraints on a single model, you can also define relational constraints using the @api.constrains decorator. These constraints can involve fields from related models and can be helpful in maintaining data consistency across different models.<\/span><\/p>\n<p style=\"font-family: &quot;Helvetica Neue&quot;; font-feature-settings: normal; font-kerning: auto; font-optical-sizing: auto; font-stretch: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-variation-settings: normal; line-height: normal; margin: 0px;\"><span style=\"font-size: medium;\"><br \/><\/span><\/p>\n<p style=\"font-family: &quot;Helvetica Neue&quot;; font-feature-settings: normal; font-kerning: auto; font-optical-sizing: auto; font-stretch: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-variation-settings: normal; line-height: normal; margin: 0px;\"><span style=\"font-size: large;\"><b>Testing Constraints:<\/b><\/span><\/p>\n<p style=\"font-family: &quot;Helvetica Neue&quot;; font-feature-settings: normal; font-kerning: auto; font-optical-sizing: auto; font-stretch: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-variation-settings: normal; line-height: normal; margin: 0px;\"><span style=\"font-size: medium;\">Always test your constraints thoroughly to ensure they work as expected and do not create any unintended restrictions on data manipulation.<\/span><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Defining Constraints: Constraints are defined in the Python code of Odoo models. Each model&hellip;<\/p>\n","protected":false},"author":2,"featured_media":72,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":"","_joinchat":[]},"categories":[5],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/pythonpower.in\/index.php\/wp-json\/wp\/v2\/posts\/14"}],"collection":[{"href":"https:\/\/pythonpower.in\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pythonpower.in\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pythonpower.in\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/pythonpower.in\/index.php\/wp-json\/wp\/v2\/comments?post=14"}],"version-history":[{"count":1,"href":"https:\/\/pythonpower.in\/index.php\/wp-json\/wp\/v2\/posts\/14\/revisions"}],"predecessor-version":[{"id":73,"href":"https:\/\/pythonpower.in\/index.php\/wp-json\/wp\/v2\/posts\/14\/revisions\/73"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pythonpower.in\/index.php\/wp-json\/wp\/v2\/media\/72"}],"wp:attachment":[{"href":"https:\/\/pythonpower.in\/index.php\/wp-json\/wp\/v2\/media?parent=14"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pythonpower.in\/index.php\/wp-json\/wp\/v2\/categories?post=14"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pythonpower.in\/index.php\/wp-json\/wp\/v2\/tags?post=14"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}