173.Django使用RESTFramework过滤、排序、分页

1.Django中的过滤filter

过滤就是获取指定条件的数据。
Django-filter库包含一个DjangoFilterBackend类,该类支持REST框架的高度可定制的字段过滤

1.1 安装

安装Django-filter

pip install django-filter==21.1

1.2 使用

配置
setting:
在这里插入图片描述

REST_FRAMEWORK = {
    # 默认的验证是按照验证列表 从上到下 的验证
    'DEFAULT_AUTHENTICATION_CLASSES': (
    # 配置JWT认证
    'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
    # 配置session_id认证
    'rest_framework.authentication.SessionAuthentication',
    # 配置默认的认证方式 base:账号密码验证
    'rest_framework.authentication.BasicAuthentication',
   ),


   # 指定过滤后端
   "DEFAULT_FILTER_BACKENDS":['django_filters.rest_framework.DjangoFilterBackend'],
}

views中添加过滤字段

`class StudentsView(generics.ListCreateAPIView):
    # 指定需要操作的数据与序列化类
    queryset = Student.objects.all()
    serializer_class = StudentSerializer
    # 添加身份验证功能
    permission_classes = [permissions.IsAuthenticatedOrReadOnly]

    # 标识匿名用户访问
    # throttle_classes = [app_throttles.AnonymousThrottle]

    # 指定视图节流类
    throttle_scope = "students"

    # 添加过滤条件
    filter_fields = {'name','sex'}
    
    # 重写新增方法,在保存Student时候关联用户
    def perform_create(self, serializer):
        serializer.save(owner=self.request.user)`

使用 单个字段多个都可以
在这里插入图片描述

2. 排序

views

from rest_framework.filters import OrderingFilter
class StudentsView(generics.ListCreateAPIView):
    # 指定需要操作的数据与序列化类
    queryset = Student.objects.all()
    serializer_class = StudentSerializer
    # 添加身份验证功能
    permission_classes = [permissions.IsAuthenticatedOrReadOnly]

    # 标识匿名用户访问
    # throttle_classes = [app_throttles.AnonymousThrottle]

    # 指定视图节流类
    throttle_scope = "students"

    # 添加过滤条件
    filter_fields = {'name','sex'}

    # 排序
    filter_backends=[OrderingFilter]
    order_fields = {'id', 'age'}

使用
在这里插入图片描述
在这里插入图片描述

3. 分页

3.1 方法1

REST framework提供了分页的支持。
通过自定义Pagination类,为视图类添加不同的分页行为,在视图中通过pagination_class属性来指明
views

from rest_framework.pagination import PageNumberPagination
# 定义分页
class LargeResultsSetPagiation(PageNumberPagination):
    page_size = 2 # 默认每页显示多少条数据
    max_page_size = 5 # 前端在控制每页显示多少条时,不可以超过5
    page_query_param = 'page' # 前端在查询字符串的关键字,指定显示第几页名字,不指定默认时page
    page_size_query_param = 'page_size' # 前端在查询关键字名字,是用来控制每页显示多少条的关键字

# 优化代码:
class StudentsView(generics.ListCreateAPIView):
    # 指定需要操作的数据与序列化类
    queryset = Student.objects.all()
    serializer_class = StudentSerializer
    # 添加身份验证功能
    permission_classes = [permissions.IsAuthenticatedOrReadOnly]

    # 标识匿名用户访问
    # throttle_classes = [app_throttles.AnonymousThrottle]

    # 指定视图节流类
    throttle_scope = "students"

    # 添加过滤条件
    filter_fields = {'name','sex'}

    # 排序
    filter_backends=[OrderingFilter]
    order_fields = {'id', 'age'}

    # 指定分页类
    pagination_class = LargeResultsSetPagiation 

3.2 方法2

分页offset

from rest_framework.pagination import PageNumberPagination, LimitOffsetPagination
class LimitOffset(LimitOffsetPagination):
    default_limit = 2 # 不指定看几条,默认就两条
    max_limit = 5 # 限制前端每次最多能看几条
    limit_query_param = 'limit'
    offset_query_param = 'offset'

# 优化代码:
class StudentsView(generics.ListCreateAPIView):
    # 指定需要操作的数据与序列化类
    queryset = Student.objects.all()
    serializer_class = StudentSerializer
    # 添加身份验证功能
    permission_classes = [permissions.IsAuthenticatedOrReadOnly]

    # 标识匿名用户访问
    # throttle_classes = [app_throttles.AnonymousThrottle]

    # 指定视图节流类
    throttle_scope = "students"

    # 添加过滤条件
    filter_fields = {'name','sex'}

    # 排序
    filter_backends=[OrderingFilter]
    order_fields = {'id', 'age'}

    # 指定分页类
    # pagination_class = LargeResultsSetPagiation
    pagination_class = LimitOffset

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

想成为数据分析师的开发工程师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值