我从ListView获得以下输出:
[<Book: Sample Title>, <Chapter: Chapter 1>, <Chapter: Chapter 2>, <Author: John Doe>]
我希望能够在表格中显示此数据,但需要根据Book
,Chapter
和Author
对其进行排序。有没有办法使用Django模板分别对这些进行分组?例如,我希望能够{{ Book.title }}
| {{ Chapter.number }}
,{{ Chapter.number }}
| {{ Author.name }}
)
views.py:
class SearchListView(ListView):
def get_queryset(self, *args, **kwargs):
query = self.request.GET.get('q', None)
books = Book.objects.filter(name__icontains=query)
chapters = Chapter.objects.filter(title__icontains=query)
authors = Author.objects.filter(name__icontains=query)
all_results = list(chain(books, chapters, authors))
return all_results