0

在 CRM 模块中有一个手机号码字段。但这是一个char我也可以添加字母字符的字段。我希望它只与数字一起使用。所以我替换mobile:fields.char('Mobile',size=20)mobile:field.integer('Mobile'),但我最多可以添加 9 位数字。有没有其他方法可以只用整数添加手机号码?我们使用 PostgreSQL,所以有数字作为一种数据类型,所以我也试过mobile:fields.numeric('Mobile',size=10)它给我一个错误:

"datatype not used in module".
4

2 回答 2

4

使用正则表达式进行验证

import re
from osv import osv,fields
class crm_lead_inherit(osv.osv):
    _name = _inherit = 'crm.lead'
    def create(self,cr,uid,vals,context=None):   
       if 'mobile' in vals and vals['mobile']:
            if re.match("^[0-9]*$", vals['mobile']) != None:
               pass
            else:
               raise osv.except_osv(_('Invalid Mobile No'),_('Please enter a valid Phone Number'))   
       return super(crm_lead_inherit, self).create(cr, uid,vals, context=context)
   def write(self,cr,uid,ids,vals,context=None):   
       if 'mobile' in vals and vals['mobile']:
            if re.match("^[0-9]*$", vals['mobile']) != None:
               pass
            else:
               raise osv.except_osv(_('Invalid Mobile No'),_('Please enter a valid Phone Number'))   
       return super(crm_lead_inherit, self).write(cr, uid,ids,vals, context=context)
crm_lead_inherit()
于 2014-02-21T06:27:54.327 回答
1

Senthilnatang 解决方案是部分正确的。此外,修改创建、写入、搜索功能也不好。所以我的意见是为现场移动使用 on_change 功能。在 xml 视图中添加 onchange

<field name="mobile" on_change="onchange_mobile(mobile)"/>

然后在 crmlead 的 python 文件中,在 crmlead 类中,

def onchange_mobile(self, cr, uid, ids, mobile, context=None):
    if not mobile:
        return {}
    mobile = mobile.replace('.','') #removes any '.' from the string
    mobile = mobile.replace(' ','') #removes space from the string
    if not mobile.isdigit():
        raise osv.except_osv(_('Invalid Mobile No'),_('Please enter a valid Phone Number'))   
    return {}

如果需要,您可以移除替换部件。

于 2014-02-24T05:39:13.507 回答