Odoo14学习笔记(5) 通过onchange机制自动计算“工龄”
第一步:引用日期处理方法:
from datetime import datetime from dateutil.relativedelta import relativedelta
第二步:添加onchange方法:
@api.onchange("workDate") def _onchange_work_date(self): for rec in self: if rec.workDate: rec.workAge = relativedelta(dt1=datetime.today(), dt2=rec.workDate).years + 1
注:多字段时onchange中逗号分隔即可,如 :
@api.onchange("columnName1","columnName2", "columnName3", ...) def _onchange_work_date(self): for rec in self: if rec.workDate: rec.workAge = relativedelta(dt1=datetime.today(), dt2=rec.workDate).years + 1
如果需要提示信息,如:
@api.onchange("workDate") def _onchange_work_date(self): for rec in self: if rec.workDate: rec.workAge = relativedelta(dt1=datetime.today(), dt2=rec.workDate).years + 1 return { 'warning':{ 'title': u'错误信息标题', 'message': u'错误详情' } }