当前位置:网站首页>分页、过滤
分页、过滤
2022-07-23 05:38:00 【ZXY_lucky】
分页
- 全局分页
- 局部分页
- 自定义分页
1.drf全局分页
在setting文件下配置
分页使用DEFAULT_PAGINATION_CLASS和PAGE_SIZE进行全局设置
# drf框架配置信息
REST_FRAMEWORK = {
# 全局分页
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 2
2.drf局部分页
局部分页时要在setting中配置
REST_FRAMEWORK = {
'PAGE_SIZE': 2
from rest_framework.pagination import PageNumberPagination
class GoodView(ModelViewSet):
queryset = Goods.objects.all()
serializer_class = GoodsSerializers
# 局部分页
pagination_class = PageNumberPagination
3.drf自定义分页
# 自定义分页
from rest_framework.pagination import PageNumberPagination
class pagination(PageNumberPagination):
max_page_size = 3 # 一页最多显示多少数据
page_size = 1 # 默认一页显示多少数据
page_query_param = 'page' # 用于指定第几页 页面参数
page_size_query_param = 'size' # 参数,用于指定一页显示多少条数据
class GoodView(ModelViewSet):
queryset = Goods.objects.all()
serializer_class = GoodsSerializers
# 自定义分页器进行分页
pagination_class = pagination
过滤
- 精准过滤
- 模糊过滤
- SearchFilter
1.精准过滤
安装:`pip install django-filter
注册
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework', # drf框架
'corsheaders', # 跨域
'kind', # 注册子应用
'django_filters', # 注册过滤器
]
配置过滤引擎
# drf框架配置信息
REST_FRAMEWORK = {
# 全局过滤
'DEFAULT_FILTER_BACKENDS': ['django_filters.rest_framework.DjangoFilterBackend']
}
视图
from django_filters.rest_framework import DjangoFilterBackend
class GoodView(ModelViewSet):
queryset = Goods.objects.all()
serializer_class = GoodsSerializers
# 局部过滤
filter_backends = [DjangoFilterBackend] # 指定过滤工具,如果全局设置过,局部不需要设置
filter_fields = ['name','kind'] # 过滤字段
2.模糊过滤
自定义过滤器
import django_filters
# 自定义过滤器
class MyFilter(django_filters.rest_framework.FilterSet):
# 价格大于等于 min_price
# NumberFilter对数值进行过滤 field_name='price过滤字段 lookup_expr='gte'过滤表达式
# 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 对字符串进行过滤
good_name = django_filters.CharFilter(field_name='name',lookup_expr='icontains')
# 元类
class Meta:
model = Goods # 指定模型类
fields = ['min_price','max_price','good_name'] # 指定过滤字段
class GoodView(ModelViewSet):
queryset = Goods.objects.all()
serializer_class = GoodsSerializers
# 模糊过滤,自定义过滤器
filter_backends = [DjangoFilterBackend]# 指定过滤工具,如果全局设置过,局部不需要设置
filterset_class = MyFilter # 指定过滤器
3.SearchFilter
将过滤器添加到全局设置中
# drf框架配置信息
REST_FRAMEWORK = {
'DEFAULT_FILTER_BACKENDS': ['rest_framework.filters.SearchFilter']
}
或添加到单个视图
from rest_framework.filters import SearchFilter
class GoodView(ModelViewSet):
queryset = Goods.objects.all()
serializer_class = GoodsSerializers
# 搜索过滤器
filter_backends = [SearchFilter] # 指定过滤工具
# 如果使用外键字段搜索,使用,外键__外键字段 进行指定
search_fields = ['name','kind__kind_name'] # 指定过滤字段
功能:过滤全部查找,根据外键查询相关数据
from rest_framework.filters import SearchFilter
from rest_framework.generics import ListAPIView
class GoodView2(ListAPIView):
queryset = Goods.objects.all()
serializer_class = GoodsSerializers2
filter_backends = [SearchFilter] # 指定使用过滤引擎
search_fields = ['kind__kind_name','kind__id','name'] # 指定过滤字段
路由
urlpatterns = [
path('good2/',views.GoodView2.as_view())
]
可以通过字符前添加字符来限制
- ^ 开始搜索
- = 完全匹配
- $ 正则表达式搜索
边栏推荐
- The 12th Blue Bridge Cup embedded design and development project
- 【无标题】
- 超链接去掉下划线代码
- JDBC database connection pool
- Huawei executives talk about the 35 year old crisis. How can programmers overcome the worry of age?
- An accident caused by MySQL misoperation, and "high availability" is not working well
- MySql语句查询某一级节点的所有子节点
- "The six programming languages I want most!"
- Pyqt5 use qpainter to draw the coordinate axis and display the scatter diagram
- 【系统问题】.NET Framework 3.5 安装错误
猜你喜欢

The 12th Blue Bridge Cup embedded design and development project

Mysql事务回滚机制与原理

Pyqt5 use qpainter to draw the coordinate axis and display the scatter diagram

超级简单的人脸识别api 只需几行代码就可以实现人脸识别

MySql语句查询某一级节点的所有子节点

LearnOpenGL - Introduction

Updated again, idea 2022.2 officially released

FFmpeg 音频编码

Project deployment (simplified version)

防止神经网络过拟合的五种方法
随机推荐
支付宝DTS架构
Gerrit 使用操作手册
达人专栏 | 还不会用 Apache Dolphinscheduler?大佬用时一个月写出的最全入门教程
sort
What does resource pooling and resource pooling mean?
Cadence learning path (VIII) PCB placement components
Updated again, idea 2022.2 officially released
面试必备之数据库专题
华为高层谈 35 岁危机,程序员如何破年龄之忧?
【无标题】
A usage exploration of entitymanagerfactory and entitymanager
Dynamic memory management
Mysql事务回滚机制与原理
知识点回顾
Activiti工作流使用之Activiti-app的安装及流程创建
pygame实现飞机大战游戏
使用cmd安装pygame
Mysql-8.0.28 user operation or user permission Operation
Filter in MATLAB
Déploiement du projet (version abrégée)