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

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

topflankerkillertopflankerkiller Posts: 2Questions: 1Answers: 0
edited February 2019 in Free community support

My problem is that my returning json is not the expecting one based on the limit that I have given to my paginator. My final goal is to do server side processing with the returned data.

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;

Answers

  • allanallan Posts: 63,480Questions: 1Answers: 10,467 Site admin

    That's the way server-side processing works. If I've understood correctly, you have server-side processing enabled, and DataTables is requesting the first 10 rows of data for display.

    Server-side processing is only useful if you have tens of thousands or rows, or more. For 11 rows, it would just introduce network latency.

    For more information please see the manual.

    Allan

  • topflankerkillertopflankerkiller Posts: 2Questions: 1Answers: 0

    @allan I have more or less around 4000 records. Without server-side processing I see network latency despite the fact that my records are not tens of thousands That's why I want to implement server-side processing. Moreover, I can not understand why my returned json is correct when my pagination number is under 10.

    Thanks for the answer.

This discussion has been closed.