Odoo14 EE条码移动端视图行组件如何新增自定义字段与值
Odoo14 EE条码移动端视图添加自定义字段解决方案
你代码里的this.page是后端条码接口组装后返回给前端的页面数据,lines内的字段完全由后端返回内容决定,按以下步骤操作即可完成自定义字段新增:
操作步骤
步骤1:给对应业务模型新增自定义字段
首先给条码关联的明细行模型(通常是stock.move.line)添加你需要的两个自定义字段:from odoo import models, fields class StockMoveLine(models.Model): _inherit = 'stock.move.line' x_custom_field1 = fields.Char(string="自定义字段1") x_custom_field2 = fields.Float(string="自定义字段2")步骤2:扩展后端条码接口,把自定义字段加入返回数据
继承条码模块的控制器,在组装返回行数据时补充自定义字段:
完成这一步后,前端拿到的from odoo.addons.stock_barcode.controllers.main import BarcodeClientController class CustomBarcodeClientController(BarcodeClientController): def _get_picking_lines(self, picking): lines = super()._get_picking_lines(picking) # 遍历每一行补充自定义字段 for line in lines: move_line = self.env['stock.move.line'].browse(line['id']) line['x_custom_field1'] = move_line.x_custom_field1 line['x_custom_field2'] = move_line.x_custom_field2 return linesthis.page.lines里就已经包含你新增的两个自定义字段了。步骤3:扩展QWeb模板渲染自定义字段
继承条码的行模板,在需要的位置渲染新增字段:<odoo> <template id="stock_barcode_lines_template_inherit" inherit_id="stock_barcode.stock_barcode_lines_template"> <!-- 根据你的需求调整xpath定位,示例是放在产品名称之后 --> <xpath expr="//div[hasclass('o_product_name')]" position="after"> <div class="o_custom_fields"> <span t-esc="line.x_custom_field1" class="ml-2"/> <span t-esc="line.x_custom_field2" class="ml-3"/> </div> </xpath> </template> </odoo>可选步骤:扩展前端JS(仅字段被过滤时需要)
如果默认的getProductLines方法过滤了自定义字段,你可以继承LinesWidget保留自定义字段:odoo.define('your_module.custom_lines_widget', function (require) { 'use strict'; const LinesWidget = require('stock_barcode.LinesWidget'); LinesWidget.include({ getProductLines: function (lines) { const res = this._super(...arguments); res.forEach(productLine => { const originLine = lines.find(l => l.id === productLine.id); productLine.x_custom_field1 = originLine.x_custom_field1; productLine.x_custom_field2 = originLine.x_custom_field2; }); return res; } }); });
注意事项
- 你的自定义模块
__manifest__.py中需要添加依赖'depends': ['stock_barcode'] - 自定义的JS文件需要加入
web.assets_backend资源列表 - 自定义的XML模板文件需要加入
data列表
内容的提问来源于stack exchange,提问作者KbiR
相关产品推荐
相关产品推荐

