首页 > 解决方案 > 如何将 LazyPaginator 与 RequestConfig 和 Table 类一起使用?

问题描述

我想可视化一个大的索引表——大到count(*)对我的用例来说太慢了。这是我的views.py代码:

import django_tables2

from projectname.models import Growth

def dashboard(request):

    class StatisticsTable(django_tables2.Table):

        class Meta:
            model = Growth

    table = StatisticsTable(Growth.objects.all())
    django_tables2.RequestConfig(
            request
    ).configure(table)
    return render(request, "plain_table.html", {'table': table,
                                                'title': 'Growth dashboard',
                                                'search': None})

我一直在寻找有关如何在django_tables2.paginators.LazyPaginator此处使用的示例,到目前为止只发现我应该将它作为paginate=in传递django_tables2.RequestConfig,但是如果我在那里传递对类的引用,我仍然会得到一个常规的分页器。在这种情况下,这个类的正确用途是什么?

标签: django-tables2

解决方案


RequestConfig(paginate={"paginator_class": LazyPaginator}).configure(table)

推荐阅读