Odoo 14自定义模块安装触发ir.actions.report.report_type相关ValueError错误
原始错误信息
ValueError: ir.actions.report.report_type: required selection fields must define an ondelete policy that implements the proper cleanup of the corresponding records upon module uninstallation. Please use one or more of the following policies: 'set default' (if the field has a default defined), 'cascade', or a single-argument callable where the argument is the recordset containing the specified option.
问题根因
你在自定义模块中扩展了Odoo原生ir.actions.report模型的report_type selection字段,新增了自定义选项,但没有给新增的选项定义ondelete卸载清理策略,而该字段为必填项,Odoo要求必填selection字段的自定义扩展选项必须指定卸载策略,避免模块卸载后产生无效数据。
排查步骤
- 检索自定义模块中所有继承
ir.actions.report模型的代码 - 定位给
report_type字段新增自定义选项的fields.Selection定义代码 - 检查该字段定义是否缺失
ondelete参数
修复方案
根据业务场景选择以下任意一种符合要求的ondelete策略即可:
方案1:set default策略
模块卸载后,所有使用你自定义report_type的报告自动回退到Odoo原生默认值qweb-pdf,适合不需要保留自定义类型关联报告的场景。
代码示例:
from odoo import models, fields class IrActionsReport(models.Model): _inherit = 'ir.actions.report' report_type = fields.Selection( selection_add=[('your_custom_type', '你自定义的报告类型名称')], ondelete={'your_custom_type': 'set default'} )
方案2:cascade策略
模块卸载后,所有使用你自定义report_type的报告直接被级联删除,适合自定义报告和模块强绑定、卸载后无需留存的场景。
代码示例:
report_type = fields.Selection( selection_add=[('your_custom_type', '你自定义的报告类型名称')], ondelete={'your_custom_type': 'cascade'} )
方案3:自定义回调函数策略
模块卸载时执行你自定义的清理逻辑,适合需要特殊处理关联报告的场景,比如批量修改为其他指定的报告类型。
代码示例:
from odoo import models, fields def _custom_uninstall_handler(records): # 自定义逻辑:将使用自定义类型的报告统一修改为qweb-html类型 records.write({'report_type': 'qweb-html'}) class IrActionsReport(models.Model): _inherit = 'ir.actions.report' report_type = fields.Selection( selection_add=[('your_custom_type', '你自定义的报告类型名称')], ondelete={'your_custom_type': _custom_uninstall_handler} )
生效操作
修改代码后重启Odoo服务,重新安装/升级自定义模块即可解决报错。
内容的提问来源于stack exchange,提问作者Berkan

