2014-02-21 57 views
0

在CRM模塊中有移動號碼字段。但它是一個char字段我也可以添加字母字符。我更喜歡它只使用數字。所以我用mobile:field.integer('Mobile')替換mobile:fields.char('Mobile',size=20),但是我可以添加最多9位數字。有沒有其他的方式只添加整數手機號碼? 我們使用PostgreSQL,所以數字作爲一個數據類型,所以我也試過mobile:fields.numeric('Mobile',size=10)它扔我一個錯誤:如何在OpenERP中的CRM模塊中僅添加整數的移動號碼

"datatype not used in module". 

回答

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() 
+1

這是正確的方法,welldone兄 –

+0

@Senthilnathang:我是Openerp新手請問我該在哪裏粘貼此代碼 – nitesh

+0

@Senthilnathang我得到這個錯誤「if re.match(」^ [ 0-9] * $「,vals ['mobile'])!=無: NameError:全局名稱're'未定義' – nitesh

1

senthilnathang解決方案部分正確。此外,修改創建,寫入,搜索功能也不太好。所以我的意見是使用移動領域的on_change功能。 在XML視圖中添加的onchange

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

然後在CRM的領先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 {} 

,如果你願意,你可以刪除替換部分。

+0

是的,你可以檢查並使用on_change也引發異常也 – senthilnathang

相關問題