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

Django drf 排序

时间:2022-10-24 17:47:04浏览次数:45  
标签:info classroom name age sex Django 排序 id drf

Django drf 排序

18.1 全局配置

配置排序类

REST_FRAMEWORK = {
    
    ......
    
    'DEFAULT_FILTER_BACKENDS': (
        'rest_framework.filters.OrderingFilter', # 注意这里不能使用django_filters.rest_framework.OrderingFilter,会报错。
    ),
}

视图

class ExampleView4(ModelViewSet):
    queryset = studentsInfo.objects.all()
    serializer_class = studentSerializer
    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=-id
HTTP 200 OK
Allow: GET, POST, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

[
    {
        "id": 17,
        "name": "胡歌",
        "sex": "男",
        "age": 30,
        "classroom": "310",
        "info": "帅帅帅"
    },
    {
        "id": 16,
        "name": "范玉才",
        "sex": "男",
        "age": 23,
        "classroom": "310",
        "info": "要做个猛男,要猛!"
    },
    {
        "id": 15,
        "name": "彭于晏",
        "sex": "女",
        "age": 0,
        "classroom": "000",
        "info": "要做个猛男,要猛!"
    },
    {
        "id": 12,
        "name": "孙明辉",
        "sex": "男",
        "age": 23,
        "classroom": "333",
        "info": "这个人很懒,什么都没有留下"
    },
    {
        "id": 5,
        "name": "闵麒良",
        "sex": "男",
        "age": 23,
        "classroom": "310",
        "info": "河工胡歌"
    },
    {
        "id": 4,
        "name": "李四",
        "sex": "男",
        "age": 24,
        "classroom": "308",
        "info": "这个人很懒,什么也没有留下"
    },
    {
        "id": 3,
        "name": "张三",
        "sex": "男",
        "age": 25,
        "classroom": "307",
        "info": "这个人很懒,什么也没有留下"
    },
    {
        "id": 2,
        "name": "小红",
        "sex": "女",
        "age": 18,
        "classroom": "301",
        "info": "这个人很懒,什么也没有留下"
    }
]

18.2 局部配置

视图

from rest_framework.filters import OrderingFilter


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

[
    {
        "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": "这个人很懒,什么也没有留下"
    },
    {
        "id": 5,
        "name": "闵麒良",
        "sex": "男",
        "age": 23,
        "classroom": "310",
        "info": "河工胡歌"
    },
    {
        "id": 12,
        "name": "孙明辉",
        "sex": "男",
        "age": 23,
        "classroom": "333",
        "info": "这个人很懒,什么都没有留下"
    },
    {
        "id": 16,
        "name": "范玉才",
        "sex": "男",
        "age": 23,
        "classroom": "310",
        "info": "要做个猛男,要猛!"
    },
    {
        "id": 2,
        "name": "小红",
        "sex": "女",
        "age": 18,
        "classroom": "301",
        "info": "这个人很懒,什么也没有留下"
    },
    {
        "id": 15,
        "name": "彭于晏",
        "sex": "女",
        "age": 0,
        "classroom": "000",
        "info": "要做个猛男,要猛!"
    }
]

标签:info,classroom,name,age,sex,Django,排序,id,drf
From: https://www.cnblogs.com/minqiliang/p/16822222.html

相关文章

  • Django drf 分页
    Djangodrf分页19.1全局配置配置分页类REST_FRAMEWORK={......#分页#'DEFAULT_PAGINATION_CLASS':'rest_framework.pagination.LimitOf......
  • 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调试界面,所以静态文件功能不能注释掉,不然......
  • 第十九关——排序(重新来过)
    那就从排序开始重新的c++程序之旅吧!2022-10-2414:08:51排序一般分为三类:选择排序、插入排序、冒泡排序堆排序、归并排序、快速排序(快排)、希尔排序基数排序、计......
  • c语言面试题(C语言面试题库,快速排序非递归)
    什么是C语言C語言是1972年左右由DennisRitchie一??人在?????室(?稱AT&T)設?開發出?淼碾?腦程式語言.他??r與KenThompson?扇斯餐??UNIX作?I系統的發展,而UNIX作?I系統(是......
  • c语言生成随机数(c语言生成随机数并排序)
    vc环境下c语言怎么生成随机数能给个0-100之间的程序最好了…r=((double)rand()/((double)(RAND_MAX)(double)(1)));请问如何用C语言写随机数产生器?给个代码谢谢!一个......
  • CF 1677D(Tokitsukaze and Permutations-冒泡排序)
    已知长度为n的排列,经过k次冒泡(每次把最大的数交换到最后)后,得到的新序列为.现在已知的某些地方的值,不知道的记,求合法原排列数。考虑和排列达成双射关系。且1次冒泡会导致......