自定义一个sort.py页面:
写入一下代码:
class lanmusort(object):
def __init__(self):
self.arr=[] def lanmuget(self,obj):
for value in obj:
if value['pid']==0:
value['lists']=[]
self.arr.append(value)
self.lanmuget_two(obj,value['id'],value['lists'])
del obj
return self.arr
def lanmuget_two(self,obj,pid,lists):
for value in obj:
if value['pid'] == pid:
value['lists'] = []
lists.append(value)
self.lanmuget_two(obj,value['id'],value['lists'])
def __del__(self):
del self.arr
数据库查询页面
view.py:(引入上面的自定义页面)
# 输出左侧栏目列表
left_lanmu = models.AdminRule.objects.order_by('sort').all().values("id","title","name","sort","is_top","pagenumber","pid",)
re_left_lanmu = lanmu.lanmusort()
relanmu = re_left_lanmu.lanmuget(left_lanmu)