问题描述
我正在尝试将查询结果按字母顺序排列,如下所示:
这适用于以下代码:
def get_context(self,request):
# Get published shows,ordered alphabetically
context = super().get_context(request)
shows = ShowPage.objects.child_of(self).live().order_by("name")
context["pages"] = [{"letter" : i,"shows" : shows.filter(name__istartswith=i.upper())}
for i in "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"]
return context
下一步是将以任意数字开头的所有节目组合为一个标记为“ 0-9”的组。
以下内容可以满足我的要求,但是它非常冗长,我想知道是否有一种我不知道的更简单的方法:
def get_context(self,ordered alphabetically
context = super().get_context(request)
shows = ShowPage.objects.child_of(self).live().order_by("name")
pages = [{"letter" : i,"shows" : shows.filter(name__istartswith=i.upper())}
for i in "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"]
digits = {"letter" : "0 - 9","shows" : []}
for index,alphabet in enumerate(pages):
if alphabet["letter"].isdigit():
for show in alphabet["shows"]:
digits["shows"] += [show]
while pages[0]["letter"].isdigit():
print(pages[0])
pages.pop(0)
pages.insert(0,digits)
context["pages"] = pages
return context
有什么想法吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)