Django Rest_framework 7(分页器、过滤器、搜索、排序)

一、分页器

1.第一种分页:
    类似于django中的分页
2.第二种分页:
    偏移分页
3.第三种分页:
    加密分页(查询速度快)
    无法跳跃
1.普通分页器[PageNumberPagination]
#一页返回的数据数,必传
paginate.page_size = 2
#自定义一页要返回的数据数,以get的形式传,key为size
paginate.page_size_query_param = 'size'
#自定义页码数的名字为p,默认为page
paginate.page_query_param = 'page'
#一页显示最大的数据数
paginate.max_page_size = 3
全局使用:
REST_FRAMEWORK = {
    'DEFAULT_PAGINATION_CLASS':  'rest_framework.pagination.PageNumberPagination',
    'PAGE_SIZE': 100  # 每页数目
}
局部使用:
# 分页的定制类
class GoodsPagination(PageNumberPagination):
    page_size = 10
    page_query_param = 'page'  # http://api.example.org/accounts/		page=4&page_size=100
    max_page_size = 100
    page_size_query_param = 'page_size'

class GoodsViewSet(ListModelMixin, RetrieveModelMixin, GenericViewSet):
    queryset = Goods.objects.all()
    serializer_class = GoodsSerializer

    # 当前类视图使用的分页器类
    pagination_class = GoodsPagination
    注意:如果在视图内关闭分页功能,只需在视图内设置pagination_class = None
2.偏移分页[LimitOffsetPagination]

(与PageNumberPagination使用相同 可以防止爬取)

#每页显示的数据条数
pagination.default_limit = 4
#要偏移的标杆,在前端以get的形式传,key为offset('可修改')
pagination.offset_query_param = 'offset'
#偏移量,,在前端以get的形式传,key为limit('可修改')
pagination.limit_query_param = 'limit'
#一页最大的显示条数
pagination.max_limit = 2
3.加密分页[CursorPagination]

(与PageNumberPagination使用相同 需要另写模块)

#按nid排序
page.ordering = 'nid'
#查询的key值
cursor_query_param = 'cursor'
#每页显示多少条
page_size = 2

二、过滤器FilterSet

1.安装
pip install django-filter
2.settings.py 中注册:
INSTALLED_APPS = [
    ...
    'django_filters',
]
3.全局使用settings.py
REST_FRAMEWORK = {
    'DEFAULT_FILTER_BACKENDS'('django_filters.rest_framework.DjangoFilterBackend',)
}
4.局部使用
设置局部 的 具体的某一个xxxViewSet
  filter_backends = (django_filters.rest_framework.DjangoFilterBackend,)  # 默认后端
  # filterset_fields= ('name','is_new') #数据库中存在的字段
  filter_class = GoodsFilter  # 如果需要数据库中没有的字段,自定义
5.新建filter.py文件
#field_name: 过滤字段名,一般应该对应模型中字段名
#lookup_expr: 查询时所要进行的操作,和ORM中运算符一致,
	contains,icontains[不区分大小写],not_contains,
	gt,gte,lt,lte,exact,not_exact,startswith,endswith

class GoodsFilter(FilterSet):
    pricemin = django_filters.NumberFilter(field_name='shop_price', lookup_expr='gte')
    pricemax = django_filters.NumberFilter(field_name='shop_price', lookup_expr='lte')
    top_category = django_filters.NumberFilter(method='top_category_filter')

    def top_category_filter(self, queryset, name, value):
        result = queryset.filter(Q(category_id=value) | Q(category__parent_category__id=value)
                                 | Q(category__parent_category__parent_category_id=value))
        return result

    class Meta:
        model = Goods
        fields = ('pricemin', 'pricemax', 'is_new')
# filters.LOOKUP_TYPES = [
#     ('', '---------'),
#     ('exact', 'Is equal to'),
#     ('not_exact', 'Is not equal to'),
#     ('lt', 'Lesser than'),
#     ('gt', 'Greater than'),
#     ('gte', 'Greater than or equal to'),
#     ('lte', 'Lesser than or equal to'),
#     ('startswith', 'Starts with'),
#     ('endswith', 'Ends with'),
#     ('contains', 'Contains'),
#     ('not_contains', 'Does not contain'),
# ]

三、搜索组件SearchFilter

使用方法
1.在视图中设置filter_backends,使用rest_framework.filter.SearchFilter过滤器,
2.前端可传的search参数需要在search_fields中指明
实例:
filter_backends = (SearchFilter,)  # 默认后端
search_fields = ('name', 'goods_brief', 'goods_desc')

四、排序OrderingFilter

使用方法
1.在视图中设置filter_backends,使用rest_framework.filter.OrderingFilter,
2.前端可传的order参数需要在ordering_fields中指明
实例:
filter_backends = (OrderingFilter,)  # 默认后端
ordering_fields = ('sold_num', 'shop_price')

五、分页,过滤,搜索,排序

pagination_class
filterset_fields/filter_class
search_fields
ordering_fields
实例
class GoodsViewSet(ListModelMixin, RetrieveModelMixin, GenericViewSet):
    queryset = Goods.objects.all()
    serializer_class = GoodsSerializer

    filter_backends = (django_filters.rest_framework.DjangoFilterBackend, SearchFilter, OrderingFilter)  # 默认后端
    
    #过滤
    # filterset_fields= ('name','is_new')
    filter_class = GoodsFilter  # 自定义

	#搜索
    search_fields = ('name', 'goods_brief', 'goods_desc')

    # 排序
    ordering_fields = ('sold_num', 'shop_price')
    
    # 当前类视图使用的分页器类
    pagination_class = GoodsPagination
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值