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方法。
Metadata
Release files for django-hugepagination 1.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| django-hugepagination-1.0.2.tar.gz | 8.2 kB | Without provenance | Details for django-hugepagination-1.0.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| django_hugepagination-1.0.2-py3-none-any.whl | Python 3 | none | any | Details for django_hugepagination-1.0.2-py3-none-any.whl |
Total release size: 17.4 kB
Release files / django-hugepagination-1.0.2.tar.gz
| Download URL | django-hugepagination-1.0.2.tar.gz |
|---|---|
| Size | 8.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9b7b4347aeb0e2bf5e1a063a7b1c67985a3ad14ff4594a649497b98f9b642e4a
|
|
BLAKE2b-256 checksum How to use checksums |
006909f683bc5e29952b266d8b02b615beb64d947ec440e6fb76aa8bef35fb3d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.11.0
|
Release files / django_hugepagination-1.0.2-py3-none-any.whl
| Download URL | django_hugepagination-1.0.2-py3-none-any.whl |
|---|---|
| Size | 9.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0e945f24264f0b5948c6f2058a63d7ee53b3e37da6202415e67978bb25fba9ee
|
|
BLAKE2b-256 checksum How to use checksums |
fe835a197ba1bf2d3e72b5683b6cad48aee29a974a0a44f42f35b906905a09cd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.11.0
|
Security for django-hugepagination 1.0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.