问题
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 _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:
return False
if not contact_number:
return {}
contact_number = rec.contact_number.replace('.','')
#removes any '.' from the string
contact_number = rec.contact_number.replace(' ','')
#removes space from the string
if not contact_number.isdigit():
return False
return {}
_constraints = [
(_check_contact_number, 'Enter valid phone number...!',
['contact_number']),
]
Kindly anyone correct me. Thank you very much
回答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:
return False
if not contact_number:
return {}
contact_number = rec.contact_number.replace('.','')
#removes any '.' from the string
contact_number = rec.contact_number.replace(' ','')
#removes space from the string
if not contact_number.isdigit():
return False
return {}
_constraints = [
(_check_contact_number, 'Enter valid phone number...!', ['contact_number']),
]
回答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 string
if rec.contact_number:
size=len(str(rec.contact_number))
if size<10:
return False
if not contact_number:
return {}
if not contact_number.isdigit():
return False
return {}
_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.
回答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:
if len(str(rec.contact_number))<10:
return False
contact_number = str(rec.contact_number).replace('.','').replace(' ','')
if not contact_number.isdigit():
return False
return True
_constraints = [
(_check_contact_number, 'Enter valid phone number...!', ['contact_number']),
]
回答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.
来源:https://stackoverflow.com/questions/32368499/what-is-wrong-with-following-code-in-python