comment_list = [] # 一级 # 获取kvm_info信息 menuManages = MenuManage.objects.all() # 将db_id存成列表,避免级别的数字重复 list_hive_db_metas = [] for menuManage in menuManages: dict_hive_db_meta = { "level": menuManage.id, "name": menuManage.name, "parent_id": menuManage.parent_id, "path": menuManage.path, "icon": menuManage.icon, "children_comments": [] } # 循环将字典添加到列表中 comment_list.append(dict_hive_db_meta) print(comment_list) comment_dict = {} for i in comment_list: comment_dict[i["level"]] = i ret = [] for comment in comment_list: parent_id = comment['parent_id'] if parent_id != 0: # 当parent_id字段不为空 comment_dict[parent_id]['children_comments'].append(comment) # parent_id值不为空,就是别人的子集,所以添加到相应父级的children_comments下. else: ret.append(comment) # 把一些不要的数据取出来, # parent_id列表,为了识别是否有下级菜单 menuManage_list = [] menuManages = MenuManage.objects.all() for menuManage in menuManages: parent_id = menuManage.parent_id menuManage_list.append(parent_id) ret就是前端要循环展示的数据