美文网首页
drf之过滤排序分页

drf之过滤排序分页

作者: 晨颜 | 来源:发表于2023-05-24 22:45 被阅读0次

    1 写查询所有出版社接口

    -可以按名字或城市  模糊 匹配
    -如果城市一样,可以按id倒序排列
    http://127.0.0.1:8000/publish/?search=上海&ordering=-id
    

    2 上述接口,带分页功能
    3 研究权限和认证源码
    高级###
    继承APIView,实现上面1,2

    分页1基本分页 # page=4&page_size=5 表示查询第4页,每页显示5条
    http://127.0.0.1:8000/publish/?search=上海&ordering=-id&page=2&page_size=2

    image.png
    分页2偏移分页#offset=3&limit=2 从第3条开始,拿2条
    http://127.0.0.1:8000/publish/?search=上海&ordering=-id&offset=1&limit=4
    image.png
    分页3游标分页
    image.png
    # page.py
    from rest_framework.pagination import PageNumberPagination,LimitOffsetPagination,CursorPagination
    class MyPageNumberPagination(PageNumberPagination):
        page_size = 1
        page_query_param = 'page'
        page_size_query_param = 'page_size' # page=4&page_size=5 表示查询第4页,每页显示5条
        max_page_size = 5
    class MyLimitOffsetPagination(LimitOffsetPagination):
        """
        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 = 2
        limit_query_param = 'limit' # limit=3  这一页取3条
        offset_query_param = 'offset' # 偏移量是多少  offset=3&limit=2  从第3条开始,拿2条
        max_limit = 5
    class MyCursorPagination(CursorPagination):
        """
        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'##实际用不到
        page_size = 3
        ordering = 'id'
    
    # views.py
    from rest_framework.viewsets import ModelViewSet
    from .models import Publish
    from .serialzier import PublishSerialzier
    from rest_framework.filters import SearchFilter,OrderingFilter
    from .page import MyPageNumberPagination,MyCursorPagination,MyLimitOffsetPagination
    # Create your views here.
    class PublishView(ModelViewSet):
    
        queryset = Publish.objects.all()
        serializer_class = PublishSerialzier
    
        filter_backends = [SearchFilter,OrderingFilter]
        search_fields = ['name','addr']#表示只在'name','addr'中查,不加search_fields 列表表示全字段查询
        # 1.内置过滤:http://127.0.0.1:8000/publish/?search=10,任意字段含10的皆能过滤出来
        # 2.内置排序 http://127.0.0.1:8000/publish/?search=1&ordering=-addr # http://127.0.0.1:8000/api/v1/books/?ordering=-price,-id
        ### 第三方过滤类
        # pip3 install django-filter
        from django_filters.rest_framework import DjangoFilterBackend
        # filter_backends+=DjangoFilterBackend          #即filter_backends = [SearchFilter,OrderingFilter,DjangoFilterBackend]
        filter_backends.append(DjangoFilterBackend )
        # filter_backends =[SearchFilter,OrderingFilter,DjangoFilterBackend]
        print(type(filter_backends))#list
        print(filter_backends)
        filterset_fields = ['name']
        # pagination_class = MyPageNumberPagination # 分页1
        pagination_class = MyLimitOffsetPagination # 分页2
        # pagination_class = MyCursorPagination # 分页3
    

    高级 继承APIView,实现上面1,2

    class PublishView(ViewSetMixin,APIView):
        def list(self,request):
            name=request.query_params.get('name')
            ordering=request.query_params.get('ordering')
            print(name,ordering)
            queryset=Publish.objects.filter(name__contains=name).order_by(ordering)
            print(queryset)
            Page=MyPageNumberPagination()
            pages=Page.paginate_queryset(queryset,request)
    
    
    
            ser = PublishSerialzier(pages, many=True)
            return Response({"code":100,'count':Page.page.paginator.count,'next':Page.get_next_link(),'previous':Page.get_next_link(),'request':ser.data})
    

    相关文章

      网友评论

          本文标题:drf之过滤排序分页

          本文链接:https://www.haomeiwen.com/subject/bbzfedtx.html