方式一:
继承基类,直接重写方法
from odoo.addons.web.controllers.main import Export
class PsExport(Export):
@http.route('/web/export/get_fields', type='json', auth="user")
def get_fields(self, model, prefix='', parent_name= '', import_compat=True, parent_field_type=None, parent_field= None, exclude=None):
return XXX
方式二:
Monkey Patch – – python运行时动态改变类的方法
例1:
from odoo.addons.web.controllers.pivot import TableExporter
@http.route('/web/pivot/export_xls', type='http', auth="user")
def export_xls(self, data, token):
return XXX
TableExporter.export_xls = export_xls
例2:
fields_view_get_origin = models.BaseModel.fields_view_get
@api.model
def fields_view_get(self, view_id=None, view_type='form', toolbar=False, submenu=False):
res = fields_view_get_origin(self, view_id=view_id, view_type=view_type, toolbar=toolbar, submenu=submenu)
if view_type == 'form' and self.env['ir.module.module'].search_count(
[('name', '=', 'rul'), ('state', '=', 'installed')]):
base_model = res.get('base_model')
fields_ids = self.env['ir.model'].search([('model', '=', base_model)])
doc = etree.XML(res['arch'])
for fields_id in fields_ids.fields_ids:
xpath = "//field[@name='%s']" % fields_id.name
if doc.xpath(xpath):
node = doc.xpath(xpath)[0]
node.set('required', '1')
setup_modifiers(node, res['fields'][fields_id.name])
res['arch'] = etree.tostring(doc, encoding='unicode')
return res
models.BaseModel.fields_view_get = fields_view_get