错误与分页记录的确切数量,因为JSON结果在Django(Error with paginating

2019-11-05 12:29发布

我的问题是,我返回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条记录。

为什么出现这种情况;

文章来源: Error with paginating the exact number of records as json result in django