在模块升级或安装时更新,执行sql语句
- 编写sql语句,并保存在sql文件中
- 将sql文件引入到__manifest__.py
{
...
'data': [
'security/ir.model.access.csv',
'views/views.xml',
'models/func_create.sql',
'views/menu.xml',
# 'views/templates.xml',
],
...
}
# odoo 各种文件的执行原理:
def convert_file(cr, module, filename, idref, mode='update', noupdate=False, kind=None, report=None, pathname=None):
if pathname is None:
pathname = os.path.join(module, filename)
ext = os.path.splitext(filename)[1].lower()
with file_open(pathname, 'rb') as fp:
if ext == '.csv':
convert_csv_import(cr, module, pathname, fp.read(), idref, mode, noupdate)
elif ext == '.sql':
convert_sql_import(cr, fp)
elif ext == '.xml':
convert_xml_import(cr, module, fp, idref, mode, noupdate, report)
elif ext == '.js':
pass # .js files are valid but ignored here.
else:
raise ValueError("Can't load unknown file type %s.", filename)
def convert_sql_import(cr, fp):
cr.execute(fp.read())