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的scheme
,host
,path
三个部分的http header。响应结果将根据http header修改。
ORIGINAL_URI_MAP = ['X-Scheme','X-Forwarded-Host','X-Original-Uri']
也可以在视图中配置属性original_uri_map
,格式通上。
视图中属性优先于配置文件。
注意事项
- 如果要保持列表显示记录的顺序和翻页显示记录的顺序一致,那么排序条件和筛选条件应当与列表查询保持一致。
- 翻页查询使用
next
和previous
方法,应当避免在获取上一条和下一条前修改当前记录的排序字段值;这种情况建议用turnpage
方法。
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django-hugepagination-1.0.2.tar.gz
.
File metadata
- Download URL: django-hugepagination-1.0.2.tar.gz
- Upload date:
- Size: 8.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9b7b4347aeb0e2bf5e1a063a7b1c67985a3ad14ff4594a649497b98f9b642e4a |
|
MD5 | 210a27a7d1c30edad6781e3064fd7b91 |
|
BLAKE2b-256 | 006909f683bc5e29952b266d8b02b615beb64d947ec440e6fb76aa8bef35fb3d |
File details
Details for the file django_hugepagination-1.0.2-py3-none-any.whl
.
File metadata
- Download URL: django_hugepagination-1.0.2-py3-none-any.whl
- Upload date:
- Size: 9.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0e945f24264f0b5948c6f2058a63d7ee53b3e37da6202415e67978bb25fba9ee |
|
MD5 | 00b89a44b555b099bcbd2c60503e8fb8 |
|
BLAKE2b-256 | fe835a197ba1bf2d3e72b5683b6cad48aee29a974a0a44f42f35b906905a09cd |