用odoo做ERP系統所用技術彙總
一.selection從資料字典中讀取資料方法
@api.model
@api.selection(‘driver_type’)
def _get_driver_type(self):
pass@api.model @api.selection('jobstatus') def _get_jobstatus(self): pass @api.model @api.selection('sex') def _get_sex(self): pass
driver_type = fields.Selection(’_get_driver_type’,
string=u’駕駛證型別’) # 1A1本 2A2本 3B1本 4B2本 5C本 job_status = fields.Selection(’_get_jobstatus’,
default=‘1’,
string=u’使用狀態’,
readonly=True) # 1啟用 0停用 sex = fields.Selection(’_get_sex’,
default=‘1’,
string=u’性別’) # 1男 2女def selection(keyword):
""" description:獲取選項欄位的值 author:qiaogang param: keyword:選項指定關鍵字 time:2019-01-10 """ if keyword: def selection_decorator(func): def wrapper(*args, **kwargs): #args[0]:是方法中的self引數 Config = args[0].env['sys.column.ddl.dtl'] records = Config.search([('ddlid.keyword', '=', keyword)]) x = [] for records in records: x.append((records.key, records.value)) return x return wrapper return selection_decorator else: raise ValueError('selection裝飾器中沒有傳入關鍵字值!')
二.利用裝飾器原理從api中讀取助記碼方式:
@api.onchange(‘driver_name’, ‘driver_mni_code’)
@api.mnicode(‘driver_name’, ‘driver_mni_code’)
def _get_mnicode(self):
passdef mnicode(name, mnicode):
“”"
description:獲取助記碼的裝飾器
author:qiaogang
param:
name:生成助記碼依賴的欄位
minicode:助記碼欄位
time:2019-01-09
“”"
if name and mnicode:
def mnicode_decorator(func):
def wrapper(*args, *kwargs):
#args[0]:是方法中的self引數
record = args[0]
iname = record[name]
code = record[mnicode]
if iname:
if code:
pass
else:
# 通過正則去掉name中的特殊字元
iname = re.sub(r’\W’, ‘’, iname)
values = {}
# 通過pinyin模組生成助記碼
imnicode = pinyin.get_initial(iname).replace(" ", “”).upper()
values[mnicode] = imnicode
args[0].update(values)
return func(*args, **kwargs)
return wrapper
return mnicode_decorator
raise ValueError(‘mnicode裝飾器中必須傳入關鍵字name和mnicode!’)
三.身份證號==駕駛證號 駕駛證改變,身份證也跟著同步改變
@api.one
@api.depends(‘driver_no’)
def _get_driver_no(self):
driver_no = self.driver_no
if driver_no:
self.id_card = driver_no
id_card = fields.Char(string=u’身份證號’, compute=’_get_driver_no’)
四. 正則方面的約束(驗證)
@api.one
@api.constrains(‘phone’)
def _check_phone(self):
phone_regex = ‘^((+?86)|((+86)))?(-|\s)?(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}KaTeX parse error: Expected 'EOF', got '\ ' at position 4: |' \̲ ̲ …)’
# id_card_regex = ‘(1\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|’
# ‘10|20|30|31)\d{3}[0-9Xx]KaTeX parse error: Expected 'EOF', got '\d' at position 10: )|(^[1-9]\̲d̲{5}\d{2}((0[1-9…)’
driver_no_regex = ‘(2\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|’
‘10|20|30|31)\d{3}[0-9Xx]KaTeX parse error: Expected 'EOF', got '\d' at position 10: )|(^[1-9]\̲d̲{5}\d{2}((0[1-9…)’
if self.phone and not re.match(phone_regex, self.phone):
raise ValidationError(‘電話號碼格式不正確!’)
# if self.id_card and not re.match(id_card_regex, self.id_card):
# raise ValidationError(‘身份證格式不正確!’)
if self.driver_no and not re.match(driver_no_regex, self.driver_no):
raise ValidationError(‘駕駛證格式不正確!’)正則
五.名字唯一性約束
_sql_constraints = [(‘unique_name’, ‘unique(driver_no)’,
‘駕駛員證號已被佔用’)]
六.預設日期是年月日時分秒,用widget改成年月日
field name=“create_date” string=“建檔日期” widget=“date”/>
七.按鈕方面問題
header>
button name=“form_display” string=“表單展示” type=“object” class=“oe_highlight”/>
button name=“job_status_0” string=“停用” type=“object” class=“oe_highlight”/>
button name=“job_status_1” string=“啟用” type=“object” class=“oe_highlight”/>
/header>
@api.multi
def job_status_0(self):
return self.write({“job_status”: “0”})
@api.multi
def job_status_1(self):
return self.write({"job_status": "1"})
@api.multi
def form_display(self):
view_name = 'view_form_bs_tr_carrier'
view = self.env['ir.model.data'].search([('name', '=', view_name)])
view_id = view.res_id
return {'type': 'ir.actions.act_window',
'view_type': 'form',
'view_mode': 'form',
'view_id': view_id,
'target': 'new',
'res_model': 'bs_tr_carrier',
'res_id': self.id,
'flags': {'initial_mode': 'edit'},
}
八.數字型別非負約束問題
@api.constrains(‘usepackqty’, ‘lowlayerpackqty’, ‘maxpackqty’)
def _check_correct(self):
if self.usepackqty < 0:
raise ValueError(‘可用存放量不能為負!’)
if self.lowlayerpackqty < 0:
raise ValueError(‘底層存放量不能為負!’)
if self.maxpackqty < 0:
raise ValueError(‘最大存放量不能為負!’)
九.