从列表中排序模板中的数据

时间:2016-09-22 23:28:53

标签: python django django-templates django-class-based-views

我从ListView获得以下输出:

[<Book: Sample Title>, <Chapter: Chapter 1>, <Chapter: Chapter 2>, <Author: John Doe>]

我希望能够在表格中显示此数据,但需要根据BookChapterAuthor对其进行排序。有没有办法使用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

0 个答案:

没有答案