通过扩展 odoo/addons/base/ir 中的ir.qweb类,来扩展实现自定义功能,复制类结构如下:
# -*- coding: utf-8 -*- import ast from urlparse import urlparse from lxml import html from .qweb import QWeb, Contextifier from .assetsbundle import AssetsBundle from lxml import etree from collections import OrderedDict from odoo import api, models, tools from odoo.tools.safe_eval import assert_valid_codeobj, _BUILTINS, _SAFE_OPCODES from odoo.http import request from odoo.modules.module import get_resource_path import json from time import time import logging _logger = logging.getLogger(__name__) class IrQWebCF(models.AbstractModel, QWeb): ''' 继承ir.qweb类,实现自定义属性的渲染输出 ''' _name = 'ir.qweb' _inherit = 'ir.qweb' def _get_field(self, record, field_name, expression, tagName, field_options, options, values): field = record._fields[field_name] field_options['tagName'] = tagName field_options['expression'] = expression field_options['type'] = field_options.get('widget', field.type) inherit_branding = options.get('inherit_branding', options.get('inherit_branding_auto') and record.check_access_rights('write', False)) field_options['inherit_branding'] = inherit_branding translate = options.get('edit_translations') and options.get('translatable') and field.translate field_options['translate'] = translate # field converter model = 'ir.qweb.field.' + field_options['type'] converter = self.env[model] if model in self.env else self.env['ir.qweb.field'] # get content content = converter.record_to_html(record, field_name, field_options) attributes = converter.attributes(record, field_name, field_options, values) return (attributes, content, inherit_branding or translate)
运行时会报QWeb、Contextifier、AssetsBundle无法import,只要改成下面这样就可以了
# -*- coding: utf-8 -*- import ast from urlparse import urlparse from lxml import html #### 下面两行 #from .qweb import QWeb, Contextifier #from .assetsbundle import AssetsBundle ### 改成 from odoo.addons.base.ir.ir_qweb.qweb import QWeb, Contextifier from odoo.addons.base.ir.ir_qweb.assetsbundle import AssetsBundle