Odoo 14社区版模块选择页面自定义方法咨询
Odoo 14 社区版:自定义修改模块选择页面
一、页面官方称谓
你截图里的页面是Odoo的应用商店模块列表页,核心由base_module_install模块(负责模块安装管理)和web模块的前端组件支撑,界面入口为顶部菜单的「Apps」。
二、需要继承的核心文件与操作方式
1. 后端Python文件(模型继承)
如果要修改模块列表的数据源、新增字段或调整业务逻辑,继承ir.module.module模型(Odoo存储模块信息的核心模型):
# 自定义模块的models/__init__.py from . import module # models/module.py from odoo import models, fields, api class IrModuleModule(models.Model): _inherit = 'ir.module.module' # 示例:新增自定义字段 custom_tag = fields.Char(string='自定义标签') # 示例:重写搜索方法过滤模块 @api.model def search(self, args, offset=0, limit=None, order=None, count=False): # 过滤掉基础核心模块 args += [('name', 'not in', ['base', 'web'])] return super(IrModuleModule, self).search(args, offset, limit, order, count)
2. 前端XML文件(视图继承)
要调整页面UI布局、新增按钮或修改显示字段,继承对应的系统视图:
- 模块列表视图:
ir.module.module.tree(树状列表)、ir.module.module.search(搜索视图) - 模块详情弹窗:
ir.module.module.form
示例XML代码(放在自定义模块的views/module_views.xml):
<?xml version="1.0" encoding="utf-8"?> <odoo> <!-- 继承模块树状列表,新增自定义字段列 --> <record id="custom_module_tree" model="ir.ui.view"> <field name="name">ir.module.module.tree.custom</field> <field name="model">ir.module.module</field> <field name="inherit_id" ref="base_module_install.ir_module_module_tree"/> <field name="arch" type="xml"> <field name="shortdesc" position="after"> <field name="custom_tag"/> </field> <!-- 在列表顶部新增自定义操作按钮 --> <xpath expr="//tree" position="inside"> <button name="action_custom_operate" string="批量标记" type="object" class="oe_highlight"/> </xpath> </field> </record> <!-- 继承搜索视图,添加自定义字段搜索项 --> <record id="custom_module_search" model="ir.ui.view"> <field name="name">ir.module.module.search.custom</field> <field name="model">ir.module.module</field> <field name="inherit_id" ref="base_module_install.ir_module_module_search"/> <field name="arch" type="xml"> <field name="name" position="after"> <field name="custom_tag"/> </field> </field> </record> </odoo>
3. 前端JS文件(复杂交互修改)
如果要实现自定义交互逻辑(比如批量操作、特殊筛选),继承web.ListController组件:
// static/src/js/module_list.js odoo.define('custom_module.module_list', function(require) { "use strict"; const ListController = require('web.ListController'); const ListView = require('web.ListView'); const viewRegistry = require('web.view_registry'); const CustomModuleListController = ListController.extend({ // 重写按钮点击事件 _onButtonClicked: function(ev) { if (ev.data.attrs.name === 'action_custom_operate') { this.do_notify('提示', '批量标记操作已触发'); } else { this._super.apply(this, arguments); } }, }); const CustomModuleListView = ListView.extend({ config: _.extend({}, ListView.prototype.config, { Controller: CustomModuleListController, }), }); viewRegistry.add('custom_module_list', CustomModuleListView); return { CustomModuleListView: CustomModuleListView, }; });
在XML中加载该JS文件:
<odoo> <template id="assets_backend" name="custom module assets" inherit_id="web.assets_backend"> <xpath expr="." position="inside"> <script type="text/javascript" src="/custom_module/static/src/js/module_list.js"/> </xpath> </template> </odoo>
三、自定义模块基础配置
确保模块的__manifest__.py声明正确依赖:
{ 'name': '自定义模块管理', 'version': '14.0.1.0.0', 'category': 'Tools', 'depends': ['base_module_install', 'web'], 'data': [ 'views/module_views.xml', ], 'assets': { 'web.assets_backend': [ 'custom_module/static/src/js/module_list.js', ], }, 'installable': True, 'auto_install': False, }
内容的提问来源于stack exchange,提问作者holydragon
相关产品推荐
相关产品推荐

