在Django中将确切的记录数分页为JSON时出错

时间:2019-02-25 09:54:23

标签: django pagination server-side paginator

我的问题是我返回的json不是基于我给分页者的限制所期望的。

例如,当我给限制11定义每页返回的结果数时,分页器在请求后仅返回10。

当我请求10以上的记录时会发生这种情况。低于10的json结果是正确的。

我的观点

class ProductSerialNumbersListJSon(LoginRequiredMixin,BaseDatatableView):
    # my model
    model = ProductSerialNumbers
    columns = ['snumber' , 'order_id','product','registration_date']
    order_columns = ['snumber','order_id','product','registration_date']
    paginate_by = 11

    def get_initial_queryset(self):
        #fetch the query list from db
        query_list=ProductSerialNumbers.objects.filter(Q(snumber__isnull=True)|Q(order_id__isnull=True)|Q (order_id__finished=0)).order_by("id")
        #declare the Paginator
        paginator = Paginator(query_list,self.paginate_by) # 11 items per page
        #getting the page number from the kwargs of the url
        page=int(self.kwargs['page'])

        try:
            result = paginator.page(page)
        except PageNotAnInteger:
            result = paginator.page(1)
        except EmptyPage:
            result = paginator.page(paginator.num_pages)

        product_serials = result.object_list.all().values_list('pk', flat=True)
        result=ProductSerialNumbers.objects.filter(pk__in=list(product_serials))
        return ProductSerialNumbers.objects.filter(pk__in=list(product_serials))

我的json结果

{"recordsTotal": 11, "recordsFiltered": 11, "draw": 0, "data": [["3", "", "test_proion", "2019-01-16"], ["55", "", "test_proion", "2019-01-16"], ["56", "", "test_proion", "2019-01-16"], ["57", "", "test_proion", "2019-01-16"], ["58", "", "test_proion", "2019-01-16"], ["59", "", "test_proion", "2019-01-16"], ["60", "", "test_proion", "2019-01-16"], ["61", "", "test_proion", "2019-01-16"], ["62", "", "test_proion", "2019-01-16"], ["63", "", "test_proion", "2019-01-16"]], "result": "ok"}

尽管recordsTotal和recordsFiltered变量都返回11,但实际的json返回10条记录。

为什么会这样?

0 个答案:

没有答案