@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)’,
‘驾驶员证号已被占用’)]
SQL Constraints:就是添加一个数据库的约束。
_sql_constraints是odoo的属性,是一个元祖的列表,每个元祖是一个数据库约束。元祖的第一个元素是约束名字,第二个元素是约束规则(postgresql约束规则),第三个参数是如果违反约束弹出来的警告信息。
六.默认日期是年月日时分秒,用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 {
'name':'表单展示',
'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
}
八.数字类型非负约束问题
@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(‘最大存放量不能为负!’)
九.
1-9 ↩︎
1-9 ↩︎