Skip to main content

django huge pagination

Project description

Hugepagination

Hugepagination提供了在Django REST framework框架下使用的分页类和模型视图单条记录翻页功能,主要针对mysql数据库百万级+数据表进行了优化。

  • HugePagination,大数据表分页类
  • TurnpageModelMixin,为视图类ViewSet混入单条记录翻页功能

依赖

  • django >= 2.2.0
  • djangorestframework >= 3.10.0
  • django-filter >= 2.2.0

安装

  • pip安装
pip install django-hugepagination
  • 源码安装
python setup.py install

分页类使用

分页类HugePagination必须与视图类ModelViewSet配合使用,后端数据库应当使用MySql数据库,可排序字段和查询条件字段应当建立索引。

from hugepagination.pagination import HugePagination

class MyViewSet(viewsets.ModelViewSet):
    queryset = MyModel.objects.all()
    serializer_class = MyModelSerializer
    pagination_class = HugePagination
    filter_backends = [DjangoFilterBackend, filters.OrderingFilter]
    ordering_fields = ['name','create_time']
    filterset_fields = ['name','create_time']

单条翻页功能(上一条,下一条)

TurnpageModelMixin为视图类ModelViewSet混入单条翻页功能,这个功能是列表视图的扩展,视图类增加如下方法:

next

查询指定记录的下一条记录,URL格式如下:

http://127.0.0.1/resources/1/next/ordering=create_time&name=taobao

返回数据格式:

{
    "name": "taobao",
    ...
    "create_time": "2020-10-10 12:12:01"
}

previous

查询指定记录的下一条记录,URL格式如下:

http://127.0.0.1/resources/1/previous/ordering=create_time&name=taobao

返回数据格式同上

turnpage

查询指定ID的记录数据,同时返回该记录的上一条和下一条查询URL,URL格式如下:

http://127.0.0.1/resources/5/turnpage/ordering=create_time&name=taobao

返回数据格式如下:

{
    "next": "http://127.0.0.1/resources/4/turnpage/ordering=create_time&name=taobao",
    "previous": "http://127.0.0.1/resources/6/turnpage/ordering=create_time&name=taobao",
    "data": {
        "name": "taobao",
        ...
        "create_time": "2020-10-10 12:12:01"        
    }
}

代码示例

from hugepagination.pagination import HugePagination, TurnpageModelMixin

class MyViewSet(viewsets.ModelViewSet, TurnpageModelMixin):
    queryset = MyModel.objects.all()
    serializer_class = MyModelSerializer
    pagination_class = HugePagination
    filter_backends = [DjangoFilterBackend, filters.OrderingFilter]
    ordering_fields = ['name','create_time']
    filterset_fields = ['name','create_time']

WEB服务器存在URL重写或者反向代理,返回URL与实际访问URL不一致,处理方法

在django配置文件中增加配置项ORIGINAL_URI_MAP,该配置是list,包含三个元素,依次指定包含源请求URL的schemehostpath三个部分的http header。响应结果将根据http header修改。

ORIGINAL_URI_MAP = ['X-Scheme','X-Forwarded-Host','X-Original-Uri']

也可以在视图中配置属性original_uri_map,格式通上。 视图中属性优先于配置文件。

注意事项

  • 如果要保持列表显示记录的顺序和翻页显示记录的顺序一致,那么排序条件和筛选条件应当与列表查询保持一致。
  • 翻页查询使用nextprevious方法,应当避免在获取上一条和下一条前修改当前记录的排序字段值;这种情况建议用turnpage方法。

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-hugepagination-1.0.0.tar.gz (7.1 kB view details)

Uploaded Source

Built Distributions

django_hugepagination-1.0.0-py3.7.egg (14.1 kB view details)

Uploaded Source

django_hugepagination-1.0.0-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file django-hugepagination-1.0.0.tar.gz.

File metadata

  • Download URL: django-hugepagination-1.0.0.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.7.7

File hashes

Hashes for django-hugepagination-1.0.0.tar.gz
Algorithm Hash digest
SHA256 63e3de3b7ef95f8f41daae55e88bf93e49d2684c470e8a58782f3b0c11ab265a
MD5 5c1df528343130fff3e6ad48e4979e4e
BLAKE2b-256 4d83fbd8c4617a1de8bb433d4753bdaa617fc50ef087087ffa2b334cb88d4080

See more details on using hashes here.

File details

Details for the file django_hugepagination-1.0.0-py3.7.egg.

File metadata

  • Download URL: django_hugepagination-1.0.0-py3.7.egg
  • Upload date:
  • Size: 14.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.7.7

File hashes

Hashes for django_hugepagination-1.0.0-py3.7.egg
Algorithm Hash digest
SHA256 7a4f4c827122eb610bb9287029315654082e86a18c16029d83f5e69ec7ef8dc2
MD5 68c28d1c07c8af08cac44abfffb4626a
BLAKE2b-256 e6e54d965732625520b222c1d00a72674773d35edfe721f3e360dac6f05229ec

See more details on using hashes here.

File details

Details for the file django_hugepagination-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: django_hugepagination-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 9.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.7.7

File hashes

Hashes for django_hugepagination-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9c03f97090c82693296db2ff3bc4f2814de1488761c9cfbf458ea57feb5f054a
MD5 81cbf286c01b861ea6194c4f61b4352b
BLAKE2b-256 4097093591ddbce144ccd5bb266b94b917c9c00e358be694d957b45dedd8f5bc

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page