Odoo 14库存Product Moves报表新增自定义字段返回undefined值问题排查
Odoo14 Product Moves报表自定义字段显示undefined问题解决方案
核心错误点
- 方法缩进错误:你编写的
_query方法缩进层级错误,不属于stockMoveLine类的成员方法,重写逻辑完全不生效,查询不会返回categ_id字段值。 - 表别名引用错误:代码中
s.categ_id的别名s在stock.move.line报表原生查询中不存在。产品分类字段categ_id存储在product.template表中,原生查询里该表的别名是product_template。 - 可选遗漏:如果未在报表前端视图中添加对应字段,就算后端返回了值,前端也无法渲染,同样会显示undefined。
修正后的代码
模型代码
class stockMoveLine(models.Model): _inherit = 'stock.move.line' categ_id = fields.Many2one('product.category', 'Product Category', readonly=True) def _query(self, with_clause='', fields={}, groupby='', from_clause=''): fields['categ_id'] = ', product_template.categ_id as categ_id' groupby += ', product_template.categ_id' return super(stockMoveLine, self)._query(with_clause, fields, groupby, from_clause)
前端视图补充代码(需添加到模块的XML文件中)
<odoo> <record id="view_move_line_tree_report_inherit" model="ir.ui.view"> <field name="name">stock.move.line.tree.report.inherit</field> <field name="model">stock.move.line</field> <field name="inherit_id" ref="stock.view_move_line_tree"/> <field name="arch" type="xml"> <field name="product_id" position="after"> <field name="categ_id" string="产品分类"/> </field> </field> </record> </odoo>
生效操作
修正代码后升级对应自定义模块,刷新浏览器缓存后即可正常查看字段值。
内容的提问来源于stack exchange,提问作者Ing
相关产品推荐
相关产品推荐

