gpt4 book ai didi

python - 以下 Python 代码有什么问题?

转载 作者:太空宇宙 更新时间:2023-11-03 15:06:11 24 4
gpt4 key购买 nike

我正在尝试为一个字段实现约束但它不会导致约束验证,而是允许保存记录而不显示任何约束消息

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

请大家指正。非常感谢

最佳答案

这段代码有一个丑陋的缩进。也许这就是原因。正确的标识看起来是这样的:

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

关于python - 以下 Python 代码有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32368499/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com