我使用 wagtail_hooks.py 文件重建 wagtail admin 中的主菜单。当我更新数据库时,主菜单会根据数据库中的新内容而变化。部分文件 wagtail_hooks.py 如下:
factories = DicFactory.objects.all()
num = len(factories)
factoryMenus = []
equipmentMenus = []
simulationMenus = []
equipmentNum = []
simulationNum = []
for fac in factories:
partURL = '/admin/home/dicequipment/?factory_id='
fullURL = partURL + str(fac.id)
factoryMenus.append(MenuItem(fac.name, fullURL, classnames='icon icon-pilcrow', order=10000))
equipments = DicEquipment.objects.filter(factory_id = fac.id)
equipmentNum.append(len(equipments))
for eq in equipments:
partURL = '/admin/home/simulation/?equipment_id='
fullURL = partURL + str(eq.id)
equipmentMenus.append(MenuItem(eq.name, fullURL, classnames='icon icon-chain-broken', order=10000))
simulations = Simulation.objects.filter(equipment_id = eq.id)
simulationNum.append(len(simulations))
for sim in simulations:
partURL = '/admin/home/simulation/inspect/'
fullURL = partURL + str(sim.id) +'/?integrityCheck=true'
simulationMenus.append(MenuItem(sim.name, fullURL, classnames='icon icon-list-ol', order=10000))
#construct the main menu
@hooks.register('construct_main_menu')
def hide_images_menu_item(request, menu_items):
menu_items[:] = [item for item in menu_items if item.name != 'images']
menu_items[:] = [item for item in menu_items if item.name != 'documents']
menu_items[:] = [item for item in menu_items if item.name != 'reports']
menu_items[:] = [item for item in menu_items if item.name != 'settings']
menu_items[:] = [item for item in menu_items if item.name != 'explorer']
menu_items[:] = [item for item in menu_items if item.label != '工厂']
menu_items[:] = [item for item in menu_items if item.label != '装置']
menu_items[:] = [item for item in menu_items if item.label != '数字模型']
menu_items[:] = [item for item in menu_items if item.label != '算例列表']
menu_items[:] = [item for item in menu_items if item.label != '算例输入']
menu_items[:] = [item for item in menu_items if item.label != '硬件环境']
menu_items[:] = [item for item in menu_items if item.label != '模拟软件']
menu_items[:] = [item for item in menu_items if item.label != '人员列表']
menu_items[:] = [item for item in menu_items if item.label != '算例输入']
menu_items.append(MenuItem('工厂', '/admin/home/dicfactory/', classnames='icon icon-folder-open-inverse', order=10000))
equipmentMenuIndex = 0
simulationMenuIndex = 0
factoryIndex = 0
equipmentIndex = 0
for i in range(num):
menu_items.append(factoryMenus[i])
for j in range(equipmentNum[factoryIndex]):
menu_items.append(equipmentMenus[equipmentMenuIndex])
equipmentMenuIndex = equipmentMenuIndex +1
for k in range(simulationNum[equipmentIndex]):
menu_items.append(simulationMenus[simulationMenuIndex])
simulationMenuIndex = simulationMenuIndex +1
equipmentIndex = equipmentIndex + 1
factoryIndex = factoryIndex + 1
数据库更改时如何自动重新加载文件 wagtail_hooks.py?希望得到您的解答和帮助。谢谢你。