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'

    @api.one
    @api.constrains('length')
    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"?>
<openerp>
    <data>
        <!-- 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"/>
                </field>
            </field>
        </record>
    </data>
</openerp>


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

Youtube Video 

4 comments:

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

    ReplyDelete
  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

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

    ReplyDelete
    Replies
    1. You may try with this

      NOTE:
      I didn't try below example.

      Constraint example in Odoo-9:

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

      Delete

ImportError: cannot import name 'utils' from 'PyPDF2'

Odoo 15: Traceback (most recent call last):   File "/usr/local/lib/python3.8/threading.py", line 932, in _bootstrap_inner     self...