编程语言
首页 > 编程语言> > 在Python中执行以下代码有什么问题?

在Python中执行以下代码有什么问题?

作者:互联网

我试图对字段实施约束
但它不会引起约束验证,而是允许保存记录而不显示任何约束消息

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

标签:odoo,odoo-8,openerp-8,python
来源: https://codeday.me/bug/20191027/1948342.html