当前位置:网站首页>Unit 15 Paging, Filtering
Unit 15 Paging, Filtering
2022-08-02 14:18:00 【czy1206527605】
一.分页
1.什么是分页
RESTThe framework includes support for customizable pagination styles.
Paging is divided into two cases:
Global paging and local paging
2.全局分页设置
The global configuration information is placedsetting里面
REST_FRAMEWORK = {
# 全局分页
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
# 指定分页引擎
'PAGE_SIZE': 2
# 指定页面大小
}
全局&Use of partial paging
http://127.0.0.1:8000/路由名/?page=数字
At the same time modify the front-end data
After parsing the datadata后加上.results
3.局部分页设置
导入分页器
from rest_framework.pagination import PageNumberPagination
使用
class GoodView(ModelViewSet):
queryset = Goods.objects.all()
serializer_class = GoodsSerializer
# 局部分页
pagination_class = PageNumberPagination
注意,Use global paging
‘DEFAULT_PAGINATION_CLASS’:‘rest_framework.pagination.PageNumberPagination’,
进行注释,保留page行.Otherwise it won't work
4.自定义分页器
使用
class GoodView(ModelViewSet):
queryset = Goods.objects.all()
serializer_class = GoodsSerializer
# 使用自定义分页器进行分页
pagination_class = pagination
paginationPager defined for yourself
自定义分页器
class pagination(PageNumberPagination):
max_page_size = 3 # 一页最多显示多少条数据
page_size = 1 # How many pieces of data are displayed on one page by default
page_query_param = 'page' # 参数,用于指定第几页
page_size_query_param = 'size' # 参数,Used to specify how many pieces of data to display overnight
自定义分页器的使用
http://127.0.0.1:8000/路由名/?page=数字&size=数字
pagefor several pages sizeSeveral lines per page
二.过滤
1.django-filter的精准过滤
before using filteringsetting里进行安装
Global filtering configuration
REST_FRAMEWORK = {
'DEFAULT_FILTER_BACKENDS': ['django_filters.rest_framework.DjangoFilterBackend']
}
Local precision filtering
1.导包
from django_filters.rest_framework import DjangoFilterBackend
2.使用
class GoodView(ModelViewSet):
queryset = Goods.objects.all()
serializer_class = GoodsSerializer
# 局部过滤
# 1.精准过滤
# filter_backends = [DjangoFilterBackend] # 指定过滤工具
# filter_fields = ['name','kind'] # 指定字段
2.django-filter的模糊过滤
Local blur filtering
导入
import django_filters
自定义过滤器
class MyFilter(django_filters.rest_framework.FilterSet):
# 价格大于等于min_price
# NumberFilterFilter on numeric types field_name过滤的字段 lookup_expr过滤的表达式
# gte 大于等于 lte 小于等于 icontains 不区分大小写的包含
min_price = django_filters.NumberFilter(field_name='price',lookup_expr='gte')
max_price = django_filters.NumberFilter(field_name='price',lookup_expr='lte')
# CharFilter Filter data of string type
name = django_filters.CharFilter(field_name='name',lookup_expr='icontains')
# 元类
class Meta:
models = Goods # 指定模型类
fields = ['min_price','max_price','name'] # 指定过滤字段
指定过滤器
# filter_backends = [DjangoFilterBackend]
# filterset_class = MyFilter
3.rest_framework的SearchFilter
全局
settingfilter engine configuration
REST_FRAMEWORK = {
'DEFAULT_FILTER_BACKENDS': ['rest_framework.filters.SearchFilter']
}
局部
class GoodView(ModelViewSet):
queryset = Goods.objects.all()
serializer_class = GoodsSerializer
filter_backends = [SearchFilter] #指定过滤工具
# If you need to search using foreign key fields,使用 外键__外键字段 进行指定
search_fields = ['name','kind__id'] # 指定过滤字段 kind__id kind__kind_name
边栏推荐
- 目标检测场景SSD-Mobilenetv1-FPN
- 浅浅写一下PPOCRLabel的使用及体验
- What are the file encryption software?Keep your files safe
- Swagger 的使用
- 【Tensorflow】AttributeError: ‘_TfDeviceCaptureOp‘ object has no attribute ‘_set_device_from_string‘
- The bad policy has no long-term impact on the market, and the bull market will continue 2021-05-19
- Sentinel源码(二)入口方法分析
- web测试和app测试的区别?
- ftp常用命令详解_iftop命令详解
- 网络剪枝(1)
猜你喜欢
随机推荐
RHCE第一天作业
【ROS】工控机的软件包不编译
网络安全第三次作业
Interview | with questions to learn, Apache DolphinScheduler Wang Fuzheng
deal!It's July 30th!
海明校验码纠错设计原理
【Tensorflow】AttributeError: ‘_TfDeviceCaptureOp‘ object has no attribute ‘_set_device_from_string‘
Break the limit of file locks and use storage power to help enterprises grow new momentum
logback源码阅读(二)日志打印,自定义appender,encoder,pattern,converter
rust使用mysql插入数据
浅浅写一下PPOCRLabel的使用及体验
Cloin 控制台乱码
FFmpeg 的AVCodecContext结构体详解
文件加密软件有哪些?保障你的文件安全
第十四单元 视图集及路由
微信小程序-最近动态滚动实现
Flask框架的搭建及入门
数据机构---第六章图---图的遍历---选择题
Shell脚本完成pxe装机配置
如何解决1045无法登录mysql服务器