Skip to content Skip to sidebar Skip to footer

What Is Wrong With Following Code In Python?

I was trying to implement a constraint for a field but instead of causing a constraint validation, it allows the record to get saved without showing any constraint message def _che

Solution 1:

This code has an ugly indents. Maybe this is the reason. The right idents looks so:

def_check_contact_number(self, cr, uid, ids, context=None):
    for rec in self.browse(cr, uid, ids, context=context):
        if rec.contact_number:
            size=len(str(rec.contact_number))
            if size<10:
                returnFalseifnot contact_number:
            return {}
            contact_number = rec.contact_number.replace('.','') 
#removes any '.' from the string
            contact_number = rec.contact_number.replace(' ','') 
#removes space from the stringifnot  contact_number.isdigit():
            returnFalsereturn {}
    _constraints = [
        (_check_contact_number, 'Enter valid phone number...!', ['contact_number']),
      ]

Solution 2:

Assuming

_constraints = [
    (_check_contact_number, 'Enter valid phone number...!',
['contact_number']),
      ]

is correct syntax, and has proper indentation, this should be your code;

def_check_contact_number(self, cr, uid, ids, context=None):

    for rec in self.browse(cr, uid, ids, context=context):

        contact_number = rec.contact_number.replace('.','') #removes any '.' from the string
        contact_number = rec.contact_number.replace(' ','') #removes space from the string
        contact_number = rec.contact_number.replace('-','') #removes any hyphens in the stringif rec.contact_number:
            size=len(str(rec.contact_number))
            if size<10:
                returnFalseifnot contact_number:
            return {}

        ifnot  contact_number.isdigit():
            returnFalsereturn {}

    _constraints = [
        (_check_contact_number, 'Enter valid phone number...!',
['contact_number']),
      ]

--If that does not work, we will need to see the entire class in order to correct this.

I do assume

_constraints = [
    (_check_contact_number, 'Enter valid phone number...!',
['contact_number']),
      ]

is incorrectly positioned as well, possibly causing the entire collapse.

Solution 3:

Here is more optimised code for constraint. make sure you restart the server and update the customised module in order to take effect of it.

def_check_contact_number(self, cr, uid, ids, context=None):
    for rec in self.browse(cr, uid, ids, context=context):
        if rec.contact_number:
            iflen(str(rec.contact_number))<10:
               returnFalse
            contact_number = str(rec.contact_number).replace('.','').replace(' ','')
            ifnot contact_number.isdigit():
                returnFalsereturnTrue

_constraints = [
    (_check_contact_number, 'Enter valid phone number...!', ['contact_number']),
]

Solution 4:

Some of the other answers sound reasonable, here is my attempt using the new Odoo ORM API:

@api.one
@api.constrains('contact_number')
def _check_contact_number(self):
    contact_number = self.contact_number.replace('.','').replace(' ','')
    if len(contact_number) < 10:
        raise exceptions.ValidationError(
            "Phone number has to contain at least 10 digits!"
        )
    if not contact_number.isdigit():
        raise exceptions.ValidationError(
            "Phone number can only contain digits, spaces and dots!"
        )

The current API for defining constrains is much nicer. You should really learn it. The old API you are using is deprecated and will eventually be removed.

Atul Arvind's tip about remembering to restart server and upgrade the particular module is also very important.

Post a Comment for "What Is Wrong With Following Code In Python?"