تخطي للذهاب إلى المحتوى
القائمة
لقد تم الإبلاغ عن هذا السؤال
6 الردود
65364 أدوات العرض

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.

الصورة الرمزية
إهمال
أفضل إجابة

Hello Willie Ho,

Please check here for

1. Python constraints

@api.constrains('dob')

     def _check_dob(self):

        current_date = (datetime.today()).strftime("%Y-%m-%d")

        c_date = datetime.strptime(current_date,"%Y-%m-%d").date()

        for record in self:

            d_date = datetime.strptime(record.dob,"%Y-%m-%d").date()

            if c_date <= d_date:

                raise ValidationError("Your DOB is should be less then today date") 

2. SQL constraints

_sql_constraints = [ ('email_unique','UNIQUE(email)','The email must be unique') ]

Hope above code help you.

Thanks & Regards,

Ankit H Gandhi.

الصورة الرمزية
إهمال
أفضل إجابة

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

أفضل إجابة

from odoo.exceptions import ValidationError

import re

@api.constrains('email') 

def validate_mail(self): 

    if self.email: 

          match = re.match('^[_a-z]+[0-9-]*(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$', self.email) 

          if match == None: 

                 raise ValidationError('Not a valid E-mail')

الصورة الرمزية
إهمال
أفضل إجابة

Constraint for emal

@api.constrains('email')   

def _validate_email(self):
        self.email.replace(" ","")        if not re.match(r"[^@]+@[^@]+\.[^@]+", self.email):
            raise exceptions.ValidationError("Please enter valid email address")


_sql_constraints = [       

     ('email_unique',        

    'UNIQUE(email)',        

"The Email must be unique"),   

]

الصورة الرمزية
إهمال
أفضل إجابة

\https://www.youtube.com/watch?v=_5r77hQ43kQ&list=UUZVWw0iLx0pFTzUzvl_XpcA&index=8

الصورة الرمزية
إهمال
أفضل إجابة

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 ')

]


@api.constrains('email')

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

الصورة الرمزية
إهمال
المنشورات ذات الصلة الردود أدوات العرض النشاط
1
مارس 19
10067
0
فبراير 16
4
0
ديسمبر 15
3294
2
مارس 15
9872
1
نوفمبر 22
4584