restful中的分页
普通分页
普通分页类似于Django中的分页
源码
class PageNumberPagination(BasePagination):
"""
A simple page number based style that supports page numbers as
query parameters. For example: http://api.example.org/accounts/?page=4
http://api.example.org/accounts/?page=4&page_size=100
"""
# The default page size.
# Defaults to `None`, meaning pagination is disabled.
page_size = api_settings.PAGE_SIZE # DjangoPaginator就是Django中的Paginator类
django_paginator_class = DjangoPaginator # Client can control the page using this query parameter.
page_query_param = 'page'
page_query_description = _('A page number within the paginated result set.') # Client can control the page size using this query parameter.
# Default is 'None'. Set to eg 'page_size' to enable usage.
page_size_query_param = None
page_size_query_description = _('Number of results to return per page.') # Set to an integer to limit the maximum page size the client may request.
# Only relevant if 'page_size_query_param' has also been set.
max_page_size = None last_page_strings = ('last',) template = 'rest_framework/pagination/numbers.html' invalid_page_message = _('Invalid page.')
源码重要的四个参数详解
# page_size是APISettings中的默认配置,用户配置则使用用户配置,未配置则使用DEFAULTS配置,默认为None
page_size = api_settings.PAGE_SIZE
# ?之后的参数,如127.0.0.1/books/?page=3
page_query_param = 'page'
# 每页最多显示的条数,如:127.0.0.1/books/?page=3&size=5
page_size_query_param = 'size'
# 超出后每页的最多显示数
max_page_size = None
简单使用
REST_FRAMEWORK = {
# 每页的显示数量
'PAGE_SIZE': 2
}
settings.py
views.py文件
from rest_framework.pagination import PageNumberPagination class Book(ViewSetMixin, APIView):
def get_all(self, request):
response = {'status': 100, 'msg': '查询成功'}
book_list = models.Book.objects.all()
# 实例化产生一个分页对象
page = PageNumberPagination()
# 第一个参数:要分页的数据,第二个参数request对象,第三个参数,当前视图对象
page_list = page.paginate_queryset(book_list, request, self)
# 再序列化的时候,用分页之后的数据
ser = mySer.BookSerializer(instance=page_list, many=True)
response['data'] = ser.data
return Response(response)
或者可以自定义类来修改数据属性
from rest_framework.pagination import PageNumberPagination class MyPageNumberPagination(PageNumberPagination):
page_size = 3
page_query_param = 'test'
page_size_query_param = 'size'
max_page_size = 5 class Book(ViewSetMixin, APIView):
def get_all(self, request):
response = {'status': 100, 'msg': '查询成功'}
book_list = models.Book.objects.all()
# 实例化产生一个分页对象
page = MyPageNumberPagination()
# 第一个参数:要分页的数据,第二个参数request对象,第三个参数,当前视图对象
page_list = page.paginate_queryset(book_list, request, self)
# 再序列化的时候,用分页之后的数据
ser = mySer.BookSerializer(instance=page_list, many=True)
response['data'] = ser.data
return Response(response)
不继承修改属性
from rest_framework.pagination import PageNumberPagination class Book(ViewSetMixin, APIView):
def get_all(self, request):
response = {'status': 100, 'msg': '查询成功'}
book_list = models.Book.objects.all()
# 实例化产生一个分页对象
# 不继承来修改对象的值
page = PageNumberPagination()
page.page_size = 2
page.page_query_param = 'bb'
# page = MyPageNumberPagination()
# 第一个参数:要分页的数据,第二个参数request对象,第三个参数,当前视图对象
page_list = page.paginate_queryset(book_list, request, self)
# 再序列化的时候,用分页之后的数据
ser = mySer.BookSerializer(instance=page_list, many=True)
response['data'] = ser.data
return Response(response)
# 会带着链接,和总共的条数(不建议用)
# return page.get_paginated_response(ser.data)
# return Response(ser.data)
View.py
偏移分页
源码
class LimitOffsetPagination(BasePagination):
"""
A limit/offset based style. For example: http://api.example.org/accounts/?limit=100
http://api.example.org/accounts/?offset=400&limit=100
"""
default_limit = api_settings.PAGE_SIZE
limit_query_param = 'limit'
limit_query_description = _('Number of results to return per page.')
offset_query_param = 'offset'
offset_query_description = _('The initial index from which to return the results.')
max_limit = None
template = 'rest_framework/pagination/numbers.html'
重要参数详解
# 默认显示数量
default_limit = api_settings.PAGE_SIZE
# 标杆值,从哪开始偏移
offset_query_param = 'offset'
# 根据标杆值的偏移量
limit_query_param = 'limit'
# 超出后限制最多的显示多少条
max_limit = None
其余用法跟普通分页相同,都是实例化对象,对象调用paginate_queryset(self, queryset, request, view=None)方法,参数解释(queryset:要分页的数据,request:request对象,view:当前视图对象)
加密分页
源码
class CursorPagination(BasePagination):
"""
The cursor pagination implementation is necessarily complex.
For an overview of the position/offset style we use, see this post:
https://cra.mr/2011/03/08/building-cursors-for-the-disqus-api
"""
cursor_query_param = 'cursor'
cursor_query_description = _('The pagination cursor value.')
page_size = api_settings.PAGE_SIZE
invalid_cursor_message = _('Invalid cursor')
ordering = '-created'
template = 'rest_framework/pagination/previous_and_next.html' # Client can control the page size using this query parameter.
# Default is 'None'. Set to eg 'page_size' to enable usage.
page_size_query_param = None
page_size_query_description = _('Number of results to return per page.') # Set to an integer to limit the maximum page size the client may request.
# Only relevant if 'page_size_query_param' has also been set.
max_page_size = None # The offset in the cursor is used in situations where we have a
# nearly-unique index. (Eg millisecond precision creation timestamps)
# We guard against malicious users attempting to cause expensive database
# queries, by having a hard cap on the maximum possible size of the offset.
offset_cutoff = 1000
重要参数解释
# 按哪个字段排序
ordering = 'id'
# ?后的参数
cursor_query_param = 'cursor'
# 每页的显示数量
page_size = api_settings.PAGE_SIZE
# 每页最多显示的条数
page_size_query_param = 'size'
# 最大显示数
max_page_size = None
最后返回时调用对象的get_paginated_response(self, data)方法,返回一个加密后的页数链接
restful中的分页的更多相关文章
- tp中使用分页技术
1 public function showList() { $m_ld = D ( 'guangxi_ld' ); $page = I ( 'get.p', 1 ); // 在配置中获取分页值 $p ...
- Oracle中经典分页代码!
在Oracle中因为没有top关键字,所以在sqlserver中的分页代码并不适用于Oracle,那么在Oracle中如何来实现分页呢? --查询所有数据 STUNO STUNAME STUAGE S ...
- 在yii中使用分页
yii中使用分页很方便,如下两种方法: 在控制器中: 1. $criteria = new CDbCriteria(); //new cdbcriteria数据库$criteria->id = ...
- [数据库]Oracle和mysql中的分页总结
Mysql中的分页 物理分页 •在sql查询时,从数据库只检索分页需要的数据 •通常不同的数据库有着不同的物理分页语句 •mysql物理分页,采用limit关键字 •例如:检索11-20条 selec ...
- LigerUi中的Grid中不分页显示(local)!
LigerUi中的Grid中不分页显示! grid为local usePager: true, //是否分页
- mongo中的分页查询
/** * @param $uid * @param $app_id * @param $start_time * @param $end_time * @param $start_page * @p ...
- springboot中使用分页,文件上传,jquery的具体步骤(持续更新)
分页: pom.xml 加依赖 <dependency> <groupId>com.github.pagehelper</groupId> <arti ...
- jdbcTemplate 后台接口中的分页
Springboot+jdbcTemplate 对查询结果列表做分页, 之前开发的小项目,数据逐渐增多,每次返回所有的查询结果,耗费性能和时间 想到做分页. 于是从简单的分页做起. jdbcTemp ...
- ListView 中 的 分页
Django Pagination 简单分页 当博客上发布的文章越来越多时,通常需要进行分页显示,以免所有的文章都堆积在一个页面,影响用户体验.Django 内置的 Pagination 能够帮助我 ...
随机推荐
- 九、文件IO——案例构建标准库
例子如下: mystdio.h #ifndef __MYSTDIO_H__ #define __MYSTDIO_H__ #include <sys/types.h> #define MYE ...
- java-solr solrj的使用
新建一个maven项目,引入依赖: <dependencies> <dependency> <groupId>org.apache.solr</groupId ...
- transition的属性变化
链接:https://www.cnblogs.com/yehui-mmd/p/5934157.html css3——transition属性和opacity属性 [transition-durat ...
- moment.js 时间戳转换
1. 一段时间以0点为标准 : 2018-03-15 00:00:00- 2018-03-21 00:00:00 let startTime = moment('2018-03-15').format ...
- TensorFlow从入门到理解
一.<莫烦Python>学习笔记: TensorFlow从入门到理解(一):搭建开发环境[基于Ubuntu18.04] TensorFlow从入门到理解(二):你的第一个神经网络 Tens ...
- 第26月第30天 srt
1. ffmpeg -i 0.mp4 -vf subtitles=0.srt 1.mp4 https://jingyan.baidu.com/article/c45ad29c73cef2051653e ...
- Java基础_0311: 数据表与简单Java类映射
数据表与简单Java类映射 现在假设有如下的关系表,现在要求实现如下的数据关联操作: 一个部门有多个雇员: 一个雇员有一个或零个领导 代码实现 class Dept { private int dep ...
- Ant使用指南
ant 使用指南 参考:http://www.cnblogs.com/hoojo/archive/2013/06/14/java_ant_project_target_task_run.html 一 ...
- python基础类知识~pymysql封装类
一简介:咱们来介绍下 DBA常用的几个类 二 具体代码 #!/usr/bin/python3import pymysqlimport sysclass DBHelper: def __init__(s ...
- java多线程面试题小结
http://www.importnew.com/12773.html http://www.cnblogs.com/fingerboy/p/5352880.html https://blog.csd ...