过滤分类结果Django

时间:2016-08-21 10:46:02

标签: django pagination django-views

我将Django中的ListView扩展为paginatedListView,如下所示

class paginatedListView(ListView):
per_page = 20

def get_queryset(self):
    qs = super(paginatedListView,self).get_queryset().order_by('-creation_datetime')
    paginator = Paginator(qs,self.per_page)
    try:
        page_num = int(self.request.GET.get('p', '0'))
    except ValueError:
        page_num = 0

    try:
        page = paginator.page(page_num + 1)
    except (EmptyPage, InvalidPage):
        page = paginator.page(paginator.num_pages)

    self.cl = InlineChangeList(self.request, page_num, paginator)
    self.paginator = paginator

    if self.cl.show_all:
        return qs
    else:
        return page.object_list

def get_context_data(self,**kwargs):
    context = super(paginatedListView,self).get_context_data(**kwargs)
    context['cl'] = self.cl
    return context

现在我有另一个扩展paginatedListView的视图,此视图的查询集应该包含遵循条件的Order实例,所以我尝试在其get_queryset()中应用过滤器

class orderList(paginatedListView):
template_name = "dashboard/reports.html"
model = Order
def get_queryset(self):
    qs = super(orderList,self).get_queryset().filter(Order_status=Order.ORDER_PLACED)
    return qs

但是这会返回一个错误,表示

Cannot filter a query once a slice has been taken.

我知道为什么Django会抛出这个错误,我知道如何使这项工作的一种方法就是在orderList中重写paginatedListView的整个代码并在分页之前应用过滤器。但还有其他优雅的方式吗?

0 个答案:

没有答案