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.
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
_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.
- Method (to check the constraint)
- The Message (Pop-up message for End User)
- List of Fields (fields to apply the constraint)
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
good post, it worked for me, Thanks! :)
ReplyDeleteActually 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
ReplyDeleteHI ,
ReplyDeleteMay I know how _constraints works in odoo9 new api.
You may try with this
DeleteNOTE:
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