【问题标题】:What is wrong with following code in Python?Python中的以下代码有什么问题?
【发布时间】:2015-09-03 06:34:14
【问题描述】:

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

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 odoo odoo-8 openerp-8


【解决方案1】:

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

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】:

    假设

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

    --如果这不起作用,我们将需要查看整个 class 以更正此问题。

    我认为

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

    位置也不正确,可能导致整个崩溃。

    【讨论】:

      【解决方案3】:

      这里是更优化的约束代码。确保您重新启动服务器并更新自定义模块以使其生效。

      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】:

        其他一些答案听起来很合理,这是我使用新的 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 更好。你真的应该学习它。您使用的旧 API 已弃用,最终将被删除。

        Atul Arvind 关于记住重启服务器和升级特定模块的提示也非常重要。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-10-10
          • 2011-02-28
          • 2017-02-17
          相关资源
          最近更新 更多