Odoo Help


This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


how to use constraint and sql constraint?

willie ho
on 10/6/14, 11:52 PM 2,657 views

my code for constraint and sql constraint is at below:

def testing(self, cr, uid, vals, context=None):
        return False;

_sql_constraints = [
        ('name_uniq', 'unique (name)','testing !')

_constraints = [(testing,'testing',['name'])]

when I save the form, it displays nothing so I think there is something wrong.

also, where do I define 'name_uniq'? I have read few example and still have no idea for it.



| 5 4 7
Bikaner, India

OpenERP Devloper at SunARC Technology

On 10/7/14, 1:30 AM

Example _sql_constraints:-

_sql_constraints = [
        ('name_uniq', 'UNIQUE (name)',  'You can not have two users with the same name !')

'name_uniq' is constraint name give any name.(no need to define)

Example _constraints:-

    def _check_name(self, cr, uid, ids, context=None):
        for val in self.read(cr, uid, ids, ['name'], context=context):
            if val['name']:
                if len(val['name']) < 6:
                    return False
        return True

    _constraints = [
        (_check_name, 'Name must have at least 6 characters.', ['name'])

Note: Based on the above example in the records name field any duplicate record or name field less than 6 character then remove the records and add the constraints in python restart the server, update the module then only it works.

well explained

on 6/14/16, 7:12 AM
On 6/14/16, 7:19 AM

Email validation is working fine ,

_sql_constraints = [

('email_uniq', 'unique(email)', 'Email id is unique change your custom email id'),

('phone_uniq','unique(phone)','Phone number is unique so change your phone number ')



def validate_email(self):

for obj in self:

if re.match("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$", obj.email) == None:

raise ValidationError("Please Provide valid Email Address: %s" % obj.email)

return True

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)


Asked: 10/6/14, 11:52 PM
Seen: 2657 times
Last updated: 6/14/16, 7:19 AM