码迷,mamicode.com
首页 > 其他好文 > 详细

restframework解析器与分页

时间:2018-11-21 18:32:33      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:mod   return   div   sel   load   generics   ret   max   framework   

解析器

导入

from rest_framework.parsers import JSONParser, FormParser, MultiPartParser, FileUploadParser

默认设置

DEFAULT_PARSER_CLASSES: (
    rest_framework.parsers.JSONParser,      # application/json
    rest_framework.parsers.FormParser,      #    application/x-www-form-urlencoded
    rest_framework.parsers.MultiPartParser  # multipart/form-data
),

补充  FileUploadParser       # */*

局部视图

from rest_framework.parsers import JSONParser,FormParser
class PublishViewSet(generics.ListCreateAPIView):
    parser_classes = [FormParser,JSONParser]
    queryset = Publish.objects.all()
    serializer_class = PublshSerializers
    def post(self, request, *args, **kwargs):
        print("request.data",request.data)
        return self.create(request, *args, **kwargs)

全局视图

REST_FRAMEWORK={
    "DEFAULT_PARSER_CLASSES":[rest_framework.parsers.FormParser,]
}

分页

导入

from rest_framework.pagination import PageNumberPagination,LimitOffsetPagination

简单分页

class MyPageNumberPagination(PageNumberPagination):
    """自定义分页的类"""
    page_size = 1                   # 每页多少条数据
    page_query_param = page       # 已什么url名分页
    page_size_query_param = size  # 临时更改每页多少页
    max_page_size = 3               # 每页最多可显示多少条数据

方式一:针对自己写的逻辑

class BookView(APIView):

    def get(self, request):
        book_list = Book.objects.all()
        # bs = BookSerializers(book_list, many=True)    # 自己写的serializer版本

        # 分页
        # pnp = MyPageNumberPagination()
        # 偏移页测试
        pnp = MyLimitOffsetPagination()
        books_page = pnp.paginate_queryset(book_list, request, self)

        bs = BookModelSerializer(books_page, many=True, context={request: request})  # ModelSerializer组件的版本
        # print(bs.data, "type-->", type(bs))
        return Response(bs.data)

方式二:针对ModelViewSet

class AuthorViewSet(viewsets.ModelViewSet):

    # 分页
    pagination_class = MyPageNumberPagination

    throttle_classes = [VisitRateThrottle, ]
    queryset = Author.objects.all()
    serializer_class = AuthorModelSerializer

偏移分页

class MyLimitOffsetPagination(LimitOffsetPagination):
    """自定义的偏移页的类"""
    default_limit = 2                # 每页显示多少条数据
    limit_query_param = limit        # 限制显示多少条数据
    offset_query_param = offset    # 偏移

 方式一:针对自己写的逻辑

class BookView(APIView):

    def get(self, request):
        book_list = Book.objects.all()
        # bs = BookSerializers(book_list, many=True)    # 自己写的serializer版本

        # 分页
        # pnp = MyPageNumberPagination()
        # 偏移页测试
        pnp = MyLimitOffsetPagination()
        books_page = pnp.paginate_queryset(book_list, request, self)

        bs = BookModelSerializer(books_page, many=True, context={request: request})  # ModelSerializer组件的版本
        # print(bs.data, "type-->", type(bs))
        return Response(bs.data)

方式二:针对ModelViewSet

class AuthorViewSet(viewsets.ModelViewSet):

    # 偏移分页
    pagination_class = MyLimitOffsetPagination

    throttle_classes = [VisitRateThrottle, ]
    queryset = Author.objects.all()
    serializer_class = AuthorModelSerializer

restframework解析器与分页

标签:mod   return   div   sel   load   generics   ret   max   framework   

原文地址:https://www.cnblogs.com/sunch/p/9996745.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!