def xtree(data):
list1 = []
# 判断data长度
if len(data) <= 0:
return list1
# 把data处理成字典
dict = {}
for i in data:
dict[i["code"]] = i
for j in data:
# 判断是否为一级
pid = j["pid"]
if pid == None:
list1.append(j)
else:
# 判断此id对应的父级有没有son
if 'son' not in dict[pid]:
# 给对应的父类添加一个键值对
# {“son”:[]}
dict[pid]['son'] = []
# 追加到key->"son"中
dict[pid]["son"].append(j)
return list1
# 无限极分类
@addr_bp.route('/citylist')
def citylist():
r = MyRedis()
citylist = r.get_str("citylist")
if not citylist:
print("查询数据库")
sql = "select * from tb_areas"
res = db.find_all(sql)
list = xtree(res)
# 存入缓存
r.set_str("citylist", json.dumps(list), 60 * 60 * 24 * 30)
else:
print("缓存执行")
list = json.loads(citylist)
return jsonify({"code": 200, "list": list})
无限极分类查询
于 2022-08-29 11:23:51 首次发布