全局拼音搜索

This commit is contained in:
李鹏宇
2026-05-25 11:07:32 +08:00
parent 1151b3e8ed
commit a2ea2bcc87
4 changed files with 3 additions and 60 deletions
@@ -11,7 +11,7 @@
"summary": "Advance Material Backend Theme, Responsive Theme, Fully functional Theme, flexible Backend Theme, fast Backend Theme, lightweight Backend Theme, Animated Backend Theme, Modern multipurpose theme, Customizable Backend Theme, Multi Tab Backend Theme Odoo",
"category": "Themes/Backend",
"version": "0.0.4",
"depends": ["base", "web", "web_enterprise", "mail", "mail_enterprise", "voip", "yuthon_base"],
"depends": ["base", "web", "web_enterprise", "mail", "mail_enterprise", "voip"],
"data": [
"security/base_security.xml",
"security/ir.model.access.csv",
@@ -36,13 +36,7 @@ class GlobalSearch(models.Model):
if self.env.user.company_id.enable_menu_search:
menu_roots = self.env['ir.ui.menu'].search([('parent_id', '=', False)])
menu_data = self.env['ir.ui.menu'].search([
('id', 'child_of', menu_roots.ids),
'|',
('name', 'ilike', query[0].lower()),
('name_pinyin', 'ilike', query[0].lower()),
('action', '!=', False)
])
menu_data = self.env['ir.ui.menu'].search([('id', 'child_of', menu_roots.ids),('name','ilike',query[0].lower()),('action','!=',False)])
if menu_data:
menu_data = menu_data._filter_visible_menus()
for menu in menu_data:
+1 -2
View File
@@ -9,5 +9,4 @@ from . import res_partner
from . import base_month_year
from . import system_logs
from . import res_users
from . import mail_thread
from . import ir_ui_menu_piyin
from . import mail_thread
-50
View File
@@ -1,50 +0,0 @@
import pypinyin
from odoo import fields, models, api
from odoo.models import BaseModel
class IrUiMenuPiyin(models.Model):
_inherit = 'ir.ui.menu'
# 添加菜单名称的拼音字段
name_pinyin = fields.Char(
string='Name Pinyin',
compute='_compute_name_pinyin',
store=True,
index=True,
help='Pinyin representation of the menu name for search purposes'
)
@api.depends('name')
def _compute_name_pinyin(self):
for record in self:
if record.name:
# 使用pypinyin生成拼音,去除空格,小写
pinyin_list = pypinyin.lazy_pinyin(record.name)
record.name_pinyin = ''.join(pinyin_list).lower()
else:
record.name_pinyin = ''
# 重写搜索方法,支持拼音搜索
@api.model
def _search(self, domain, offset=0, limit=None, order=None, access_rights_uid=None):
# 检查是否有拼音搜索条件
new_domain = []
for condition in domain:
if isinstance(condition, (list, tuple)) and len(condition) == 3:
field, operator, value = condition
if field == 'name' and operator in ['ilike', 'like']:
# 添加拼音搜索条件
new_domain.append('|')
new_domain.append(('name', operator, value))
new_domain.append(('name_pinyin', operator, value))
else:
new_domain.append(condition)
else:
new_domain.append(condition)
if not new_domain:
new_domain = domain
return super()._search(new_domain, offset=offset, limit=limit, order=order, access_rights_uid=access_rights_uid)