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 能够帮助我 ...
随机推荐
- hackrf入门
http://www.hackrf.net/hackrf%E4%B8%8Egnuradio%E5%85%A5%E9%97%A8%E6%8C%87%E5%8D%97/
- Java SE之正则表达式三:替换
/** * * @author Zen Johnny * @date 2018年4月29日 下午4:31:07 * */ package demo.regex; public class RegexR ...
- Java Map在遍历过程中删除元素
Java中的Map如果在遍历过程中要删除元素,除非通过迭代器自己的remove()方法,否则就会导致抛出ConcurrentModificationException异常.JDK文档中是这么描述的: ...
- Django REST Framework API Guide 01
之前按照REST Framework官方文档提供的简介写了一系列的简单的介绍博客,说白了就是翻译了一下简介,而且翻译的很烂.到真正的生产时,就会发现很鸡肋,连熟悉大概知道rest framework都 ...
- python - 文件系统和文件
文件系统和文件 文件系统是os用于明确磁盘或分区上的文件的方法和数据结构--即在磁盘上组织文件的方法 计算机文件,是存储在某种长期储存设备或临时存储设备中的一段数据流,并且 ...
- Spring重温(二)--Spring JavaConfig
1.从Spring 3起,JavaConfig功能已经包含在Spring核心模块,它允许开发者将bean定义和在Spring配置XML文件到Java类中.但是,仍然允许使用经典的XML方式来定义bea ...
- openstack Q版部署-----安装报错问题
1.实例开机提示找不到磁盘Booting from Hard Disk... GRUB. 开启 CPU 虚拟化支持. 将计算节点 nova.conf 配置修改如下即可: [libvirt] cpu_m ...
- python,获取用户输入,并且将输入的内容写到.txt
该功能缺点是必须保证该文件不存在的情况才会成功 f=open('E:/mywork/保存文件.txt','x') def userwrite(code): if code=='w': f.close( ...
- flirtlib 测试过程
一. 安装flirtlib 1. 安装必要的依赖库 Boost >= 1.36 (submodules math and graph) 这个有了 Qt4 (for the gui)这个也有了 Q ...
- caffe调试
这里说一下关于如何进行Caffe的调试: 参考:https://blog.csdn.net/tsyccnh/article/details/51679121 简要说一下步骤: (1)首先对libcaf ...