使用Django的分页功能中的错误“ count()恰好接受2个参数(给定1个)”

时间:2019-02-20 11:07:35

标签: django pagination django-pagination

尽管我已经成功地从数据库中获取了数据并创建了我的分页器,但我发现自己努力返回数据并最终以json格式显示数据。

我发现这篇帖子https://simpleisbetterthancomplex.com/tutorial/2016/08/03/how-to-paginate-with-django.html很有用(尽管就我而言,我正在使用基于类的方法,因为我希望将数据表的服务器端处理作为最终目标。)

我的网址:

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

我的观点:

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)

我的回溯:

    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

1 个答案:

答案 0 :(得分:1)

此处paginator.page(..)返回django.core.paginator.Page的实例。但是您需要返回ProductSerialNumbers的queryset。您可以这样做:

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