Error with paginating the exact number of records as json result in django

六月ゝ 毕业季﹏ 提交于 2019-12-11 16:49:39

问题


My problem is that my returning json is not the expecting one based on the limit that I have given to my paginator.

For example, when I am giving the limit 11 to define the number of my returning results per page, after the request the paginator returns only 10.

This happens when I am requesting records above 10. Below 10 the json result is right.

my view

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))

my json result

{"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"}

Despite the fact that the recordsTotal and the recordsFiltered variable are returning both 11, the actual json returns 10 records.

Why this happening;

来源:https://stackoverflow.com/questions/54863503/error-with-paginating-the-exact-number-of-records-as-json-result-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!