首页 > 其他分享 >Django drf 分页

Django drf 分页

时间:2022-10-24 17:46:46浏览次数:90  
标签:info 分页 name age Django filter id drf

Django drf 分页

19.1 全局配置

配置分页类

REST_FRAMEWORK = {
    
    ......
    
    # 分页
    #'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination', # 偏移量分页器
    'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination', # 页码分页器
    # 上面两种分页器二选一,个人偏好页码分页器
    'PAGE_SIZE': 3, # 每页数据条数
}

视图

# 全局分页的话,视图无需添加新的配置项,只要视图能返回多条数据并且支持drf分页就行

class ExampleView4(ModelViewSet):
    queryset = studentsInfo.objects.all()
    serializer_class = studentSerializer
    filter_backends = [OrderingFilter]
    ordering_fields = ["id", "age"]

路由

from . import views
from rest_framework.routers import DefaultRouter

router = DefaultRouter()
router.register("filter", views.ExampleView4, basename="filter")
urlpatterns = [
    
] + router.urls

访问效果(偏移量分页器)

GET /opt/filter/?ordering=-age
HTTP 200 OK
Allow: GET, POST, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "count": 8,
    "next": "http://127.0.0.1:8000/opt/filter/?limit=3&offset=3&ordering=-age",
    "previous": null,
    "results": [
        {
            "id": 17,
            "name": "胡歌",
            "sex": "男",
            "age": 30,
            "classroom": "310",
            "info": "帅帅帅"
        },
        {
            "id": 3,
            "name": "张三",
            "sex": "男",
            "age": 25,
            "classroom": "307",
            "info": "这个人很懒,什么也没有留下"
        },
        {
            "id": 4,
            "name": "李四",
            "sex": "男",
            "age": 24,
            "classroom": "308",
            "info": "这个人很懒,什么也没有留下"
        }
    ]
}

访问效果(页码分页器)

GET /opt/filter/?ordering=-age
HTTP 200 OK
Allow: GET, POST, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "count": 8,
    "next": "http://127.0.0.1:8000/opt/filter/?ordering=-age&page=2",
    "previous": null,
    "results": [
        {
            "id": 17,
            "name": "胡歌",
            "sex": "男",
            "age": 30,
            "classroom": "310",
            "info": "帅帅帅"
        },
        {
            "id": 3,
            "name": "张三",
            "sex": "男",
            "age": 25,
            "classroom": "307",
            "info": "这个人很懒,什么也没有留下"
        },
        {
            "id": 4,
            "name": "李四",
            "sex": "男",
            "age": 24,
            "classroom": "308",
            "info": "这个人很懒,什么也没有留下"
        }
    ]
}

19.2 局部配置

配置文件

REST_FRAMEWORK = {
	......
    'PAGE_SIZE': 3,
}

视图

from rest_framework.filters import OrderingFilter
from rest_framework.pagination import PageNumberPagination


class ExampleView4(ModelViewSet):
    queryset = studentsInfo.objects.all()
    serializer_class = studentSerializer
    filter_backends = [OrderingFilter]
    ordering_fields = ["id", "age"]
    pagination_class = PageNumberPagination

路由

from . import views
from rest_framework.routers import DefaultRouter

router = DefaultRouter()
router.register("filter", views.ExampleView4, basename="filter")
urlpatterns = [
    
] + router.urls

访问效果

GET /opt/filter/?ordering=id
HTTP 200 OK
Allow: GET, POST, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "count": 8,
    "next": "http://127.0.0.1:8000/opt/filter/?ordering=id&page=2",
    "previous": null,
    "results": [
        {
            "id": 2,
            "name": "小红",
            "sex": "女",
            "age": 18,
            "classroom": "301",
            "info": "这个人很懒,什么也没有留下"
        },
        {
            "id": 3,
            "name": "张三",
            "sex": "男",
            "age": 25,
            "classroom": "307",
            "info": "这个人很懒,什么也没有留下"
        },
        {
            "id": 4,
            "name": "李四",
            "sex": "男",
            "age": 24,
            "classroom": "308",
            "info": "这个人很懒,什么也没有留下"
        }
    ]
}

19.3 自定义分页类

定义分页类

class StudentPageNumberPagination(PageNumberPagination):
    page_query_param = "page"  # 查询字符串中代表页码的变量名
    page_size_query_param = "size"  # 查询字符串中代表每一页数据量的变量名
    page_size = 2  # 每一页数据量
    max_page_size = 8  # 每一页最大数据量

视图

from rest_framework.filters import OrderingFilter
from .paginations import StudentPageNumberPagination

class ExampleView4(ModelViewSet):
    queryset = studentsInfo.objects.all()
    serializer_class = studentSerializer
    filter_backends = [OrderingFilter]
    ordering_fields = ["id", "age"]
    pagination_class = StudentPageNumberPagination

路由

from . import views
from rest_framework.routers import DefaultRouter

router = DefaultRouter()
router.register("filter", views.ExampleView4, basename="filter")
urlpatterns = [
    
] + router.urls

访问效果

GET /opt/filter/?ordering=id
HTTP 200 OK
Allow: GET, POST, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "count": 8,
    "next": "http://127.0.0.1:8000/opt/filter/?ordering=id&page=2",
    "previous": null,
    "results": [
        {
            "id": 2,
            "name": "小红",
            "sex": "女",
            "age": 18,
            "classroom": "301",
            "info": "这个人很懒,什么也没有留下"
        },
        {
            "id": 3,
            "name": "张三",
            "sex": "男",
            "age": 25,
            "classroom": "307",
            "info": "这个人很懒,什么也没有留下"
        }
    ]
}

可以看到,每页的数据量变成了两条,由此可见,分页组件采用了我们自己定义的分页类

标签:info,分页,name,age,Django,filter,id,drf
From: https://www.cnblogs.com/minqiliang/p/16822231.html

相关文章

  • drf 异常处理
    异常处理20.1异常示例视图classExceptionView(APIView):defget(self,request):a=1/0returnResponse({"message":"ok"})路由fromdja......
  • drf 生成接口文档
    生成接口文档21.1coreapi的使用安装依赖pipinstallcoreapi配置INSTALLED_APPS=['django.contrib.admin','django.contrib.auth','django.contri......
  • Django drf 权限
    Djangodrf权限15.1权限身份验证或身份识别本身通常不足以获取信息或代码的访问权限。因此,请求访问的实体必须具有授权。—AppleDeveloperDocumentation连同认证......
  • drf视图中调用的请求和响应类
    drf视图中调用的请求和响应类8.1request如果使用视图的时候使用的是drf中的视图(如:APIView),而不是django的视图(View),那么视图中调用的请求对象request不再是原来的req......
  • 使用DRF实现五个api接口
    使用DRF实现五个api接口2.1新建appdjango-adminstartappapp012.2配置环境#注册app01和rest_framework,由于drf提供了web调试界面,所以静态文件功能不能注释掉,不然......
  • django中APIView里的dispatch和as_view方法分析
    位置:fromrest_framework.viewsimportAPIView继承APIView类视图形式的路由:path('booksapiview/',views.BooksAPIView.as_view()),#在这个地方应该写个函数内存地址......
  • ubuntu+Django + nginx + uwsgi 部署
    ubuntu+Django+nginx+uwsgi部署0.前期准备注意:以下几件事都必须在激活虚拟环境下完成运行以下命令生成项目所需的依赖列表,会在项目根目录生成一个requirements.......
  • Django Admin save 重写 保存
    在djangoadmin管理控制台中,用户按下“Saveandaddanother”,表单的字段值仍然填充最后插入的值并且保存在ModelAdmin中添加选项save_as=True即可这将只对现有对象......
  • 13. MyBatis的分页插件
    一、为什么要使用分页插件  如果不使用分页插件,那需要我们在写SQL语句的时候手动实现分页,SQL语句的编写如下:select字段名1,字段名2,...from表名where查询条件......
  • 1 django配置前的介绍
    一、django配置前的介绍1.创建虚拟环境2.下载django框架在pycharm页面按Alt+F12,弹出terminal终端窗口,在其中输入pipinstall-ihttps://pypi.douban.com/simple/dja......