Wednesday, 10 December 2014

How to create constraints in OpenERP v7, Odoo 8,9, and 10 ?

This will help you to create a constraints in OpenERP.

_constraint is a pre-define field in OpenERP. It is used for adding a constraint on the object.

It takes list of tuple as its argument. The tuple inside the list contains three parameter.
  1. Method (to check the constraint)
  2. The Message (Pop-up message for End User)
  3. List of Fields (fields to apply the constraint)
_constraint will fire if the condition returns False on creation and updation of the record and display the message.

Here is an example of integer data-type. It's fire a constraint if length is not positive.

Here is .py side code:

Old API:
from openerp.osv import fields, osv
class res_partner(osv.Model):
    _inherit = 'res.partner'

    _columns = {       
        'length': fields.integer('Length', size=64),

    def _check_number(self, cr, uid, ids, context=None):
        for partner in self.browse(cr, uid, ids, context=context):
            if partner.length < 0:
                return False
        return True    
    _constraints = [
        (_check_length, 'Length must be Positive.', ['length'])

New API:

from openerp.exceptions import ValidationError
from openerp import api, fields, models, _

class Partner(models.Model):
    _inherit = 'res.partner'
    def _check_length(self):
        if self.length < 0:
            raise ValidationError("Length must be Positive.")

    length = fields.integer('Length', size=64)

Here is .xml side code:

<?xml version="1.0"?>
        <!-- res partner form view-->
        <record id="view_res_partner_extended_form" model="ir.ui.view">
            <field name="name">res.partner.extended.form.view</field>
            <field name="model">res.partner</field>
            <field name="inherit_id" ref="base.view_partner_form"/>
            <field name="arch" type="xml">
                <field name="email" position="after">
                    <field name="length"/>

I hope you like this article. Share your views to improve content. Happy Learning !!!

Youtube Video 


  1. good post, it worked for me, Thanks! :)

  2. Actually your examples help me a lot, i tried that with small examples , i have one doubt is it possible to check multiple condition ?? Please give an example , if there are three rooms A,B,C and if A is booked at 27th jun, and end with 28 jun, and another booking will not be able to do with that date, how it is possible to do with constraints??Give me an example

  3. HI ,
    May I know how _constraints works in odoo9 new api.

    1. You may try with this

      I didn't try below example.

      Constraint example in Odoo-9:

      @constraint("Name and Description must be different")
      @depends('name', 'description')
      def _check_name_des(self):
      return != self.description


ImportError: No module named googleapiclient

Recently, I have faced ImportError: No module named googleapiclient and I would like to share knowledge to fix that problem. Open termina...