Search code examples
djangopaginationdjango-pagination

Error "count() takes exactly 2 arguments (1 given)" in pagination function with django


Despite the fact that I have fetched successfully the data from the db and created my paginator, i find myself struggling returning the data and finally show the data in json format.

I found useful this post https://simpleisbetterthancomplex.com/tutorial/2016/08/03/how-to-paginate-with-django.html (despite the fact that in my case I am using class based approach because I want as final goal to have server side processing of my datatable.)

My url:

url(r'^warehouse_tabledata/(?P<page>\d+)/$',views.ProductSerialNumbersListJSon.as_view(), name='warehouse_list_json'),

My view:

class ProductSerialNumbersListJSon(LoginRequiredMixin,BaseDatatableView):
# my model
model = ProductSerialNumbers
columns = ['snumber' , 'order','product','registration_date']
order_columns = ['snumber','order','product','registration_date']
max_display_length = 100

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")
    paginator = Paginator(query_list,13) #13 items per page
    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('id', flat=True)
    return ProductSerialNumbers.objects.filter(pk__in=product_serials)

My traceback:

    NotSupportedError at /warehouse_tabledata/2/
(1235, "This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'")
Request Method: GET
Request URL:    http://73.neurosynthesis.com:9001/warehouse_tabledata/2/
Django Version: 1.11.16
Exception Type: NotSupportedError
Exception Value:    
(1235, "This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'")
Exception Location: /usr/local/lib/python2.7/dist-packages/django_datatables_view/base_datatable_view.py in handle_exception, line 265
Python Executable:  /usr/bin/python
Python Version: 2.7.12
Python Path:    
['/var/www/vhosts/intranet.health-nutrition.gr/farmakeio',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-x86_64-linux-gnu',
 '/usr/lib/python2.7/lib-tk',
 '/usr/lib/python2.7/lib-old',
 '/usr/lib/python2.7/lib-dynload',
 '/usr/local/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages']
Server time:    Fri, 22 Feb 2019 10:02:22 +0200
Traceback Switch to copy-and-paste view
/usr/local/lib/python2.7/dist-packages/django/core/handlers/exception.py in inner
            response = get_response(request) ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py in _get_response
                response = self.process_exception_by_middleware(e, request) ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py in _get_response
                response = wrapped_callback(request, *callback_args, **callback_kwargs) ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/views/generic/base.py in view
            return self.dispatch(request, *args, **kwargs) ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/contrib/auth/mixins.py in dispatch
        return super(LoginRequiredMixin, self).dispatch(request, *args, **kwargs) ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/views/generic/base.py in dispatch
        return handler(request, *args, **kwargs) ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django_datatables_view/mixins.py in get
            func_val = self.get_context_data(**kwargs) ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django_datatables_view/base_datatable_view.py in get_context_data
            return self.handle_exception(e) ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django_datatables_view/base_datatable_view.py in handle_exception
        raise e ...
▶ Local vars

Solution

  • here paginator.page(..) returns a instance of django.core.paginator.Page. But you need to return queryset of ProductSerialNumbers. You can do that like this:

    def get_initial_queryset(self):
        query_list=ProductSerialNumbers.objects.filter(Q(snumber__isnull=True)|Q(order_id__isnull=True)|Q (order_id__finished=0)).order_by("id")
        paginator = Paginator(query_list,13) #13 items per page
        page=int(self.kwargs['page'])
    
        try:
            page_obj = paginator.page(page)
        except PageNotAnInteger:
            page_obj = paginator.page(1)
        except EmptyPage:
            page_obj = paginator.page(paginator.num_pages)
    
        product_serials = page_obj.object_list.all().values_list('pk', flat=True)
        return ProductSerialNumbers.objects.filter(pk__in=list(product_serials))