Files
gzth/yuthon_base/models/ir_ui_menu_piyin.py
T
2026-05-25 11:02:43 +08:00

51 lines
1.8 KiB
Python

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)