继承APIView类想要实现分页效果,就需要用到分页类中的paginate_queryset方法(分页处理)和get_paginated_response方法(获取处理好的Response对象) 视图类编写: classBookView(APIView):defget(self, request, *args, **kwargs):# 数据库数据books = Book.objects.all()# 分页类对象paginator = CommonPageNumberPaginat...
分页器classPageNumList3(CursorPagination): cursor_query_param='tip'page_size= 2ordering='-id'视图函数classBookApiView(APIView):defget(self, request, *args, **kwargs): query_list=models.Books.objects.all() page=PageNumList3()#在数据库中获取分页的数据page_list = page.paginate_queryset(quer...
class PageBookView(APIView): def get(self, request): queryset = Book.objects.all() # 先实例化分页器对象 page_obj =改这里就可以了 # 用我自己的分页器调用分页方法进行分页 page_data = page_obj.paginate_queryset(queryset, request) # 序列化我们分页好的数据 ser_obj = BookSerializer(page_dat...
继承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&of...
也可通过自定义Pagination类,来为视图添加不同分页行为。在视图中通过pagination_clas属性来指明 classLargeResultsSetPagination(PageNumberPagination):page_size=1000page_size_query_param='page_size'max_page_size=10000classBookDetailView(RetrieveAPIView):queryset=BookInfo.objects.all()serializer_class=BookInfoSeria...
1. django使用DRF进行分页 drf框架中以及集成了相关的分页类,可以根据自己的需求添加相关的返回值。 相关后端实现代码部分 from rest_framework.pagination import PageNumberPagination #自定义自己的分页类 class MyPagination(PageNumberPagination): page_size = 15 #每一页显示的数量 ...
#父类ListAPIView是只有列表功能,如果父类变为ListCreateAPIView,那么既可以看列表,也可以改列表这个类名字是根据这个类作用起的名字。继承父类可以根据需求,现在是只是显示列表,所以直接继承这个就可以了。 代码语言:javascript 复制 classProductListView(generics.ListAPIView):'''产品列表'''将数据库里面的数据都取出...
class ExampleView(APIView): throttle_classes = (UserRateThrottle,) ... 可选限流类 1) AnonRateThrottle 限制所有匿名未认证用户,使用IP区分用户。 使用DEFAULT_THROTTLE_RATES['anon'] 来设置频次 2)UserRateThrottle 限制认证用户,使用User id 来区分。使用DEFAULT_THROTTLE_RATES['user'] 来设置频次 ...
APIView:DRF提供的所有视图的基类,继承View并扩展,具备了身份认证、权限检查、流量控制等功能。 GenericAPIView:对APIView更高层次的封装,例如增加分页、过滤器 GenericViewSet:继承GenericAPIView和ViewSet ViewSet:继承APIView,并结合router自动映射路由 ModelViewSet:继承GenericAPIView和五个扩展类,封装好各种请求,更加完善,...
# views.pyfromrest_framework.genericsimportGenericAPIViewfromrest_framework.responseimportResponseclassUserView(GenericAPIView):# 数据库查询!queryset=models.UserInfo.objects.filter(status=True)# 序列化类!serializer_class=序列化类# 分页组件 也可以取配置文件中读取pagination_class=分页组件# 条件组件 也可以取...