• 分页、排序、过滤。


    过滤的时候需要下载:
    pip install django-filter

    序列化:
    ModelSerializer:

      from rest_framework import serializers
      from demoapp import models as dm1model
      class UserSer(serializers.ModelSerializer):
      class Meta:
      # 指定模型类
      model = dm1model.User
      # 指定序列化字段
      fields = "__all__" #所有字段
      fields = ("username","home") #其中某⼏个字段
      # 序列化深度,是否序列化外键
      depth = 1
      # 只序列化字段
      read_only_fields = ('id', 'bread', 'bcomment')
      # 额外参数,修改字段设置
      extra_kwargs = {
            'age': {'min_value': 0, 'required': True},
            'hight': {'min_value': 0, 'required': True},
      }
    

    路由:
    urls.py:

      from django.urls import path, include, re_path
      from app03.views import *
      from rest_framework.routers import DefaultRouter
      router = DefaultRouter()
      router.register('user', UserModelView)
      urlpatterns = [
          path("", include(router.urls)),
          # path('adduser/', UserModelView.as_view()),
      ]
    

    过滤+排序

    settings.py:

      INSTALLED_APPS = [
       ...
       'django_filters', # 需要注册应⽤,
      ]
      REST_FRAMEWORK = {
          'DEFAULT_FILTER_BACKENDS':
              (
                  'django_filters.rest_framework.DjangoFilterBackend',  # 过滤
                  'rest_framework.filters.OrderingFilter',  # 排序
              )
      }
    

    views.py:

      from rest_framework.viewsets import ModelViewSet
      from userapp import models as userapp
      from app03 import serializers as app03
      from django_filters.rest_framework import DjangoFilterBackend  # Django自带的过滤
      from rest_framework.filters import OrderingFilter  # Django自带的排序
    
    
      class UserModelView(ModelViewSet):
           queryset = userapp.User.objects.all()
           serializer_class = app03.UserSer
           # filter_backends = [OrderingFilter,DjangoFilterBackend]
           ordering_fields = ("age", "hight")
           filter_fields = ("username", "age", "home")
    
      路由格式:http://127.0.0.1:8000/app03/user/?ordering=hight&home=羊村
    

    过滤:

    settings.py:

      REST_FRAMEWORK = {
          'DEFAULT_FILTER_BACKENDS':
              (
                  'django_filters.rest_framework.DjangoFilterBackend',  # 过滤
              )
      }
    
      views.py:
      from rest_framework.viewsets import ModelViewSet
      from userapp import models as userapp
      from app03 import serializers as app03
      from django_filters.rest_framework import DjangoFilterBackend  # Django自带的过滤
    
    
      # 使⽤全局过滤器
      class UserModelView(ModelViewSet):
          queryset = userapp.User.objects.all()
          serializer_class = app03.UserSer
          filter_fields = ("username", "age", "home")
    
      # 局部使⽤过滤器
      class UserModelView(ModelViewSet):
          queryset = userapp.User.objects.all()
          serializer_class = app03.UserSer
          filter_backends = [DjangoFilterBackend]  # 申明配置过滤器
          filter_fields = ("username", "age", "home")
    
      路由格式:http://127.0.0.1:8000/app03/user/?age=3&home=1
    

    排序

    settings.py:

      REST_FRAMEWORK = {
          'DEFAULT_FILTER_BACKENDS':
              (
                  'rest_framework.filters.OrderingFilter',  # 排序
              )
      }
    

    views.py:

      from rest_framework.viewsets import ModelViewSet
      from userapp import models as userapp
      from app03 import serializers as app03
      from rest_framework.filters import OrderingFilter  # Django自带的排序
    
      # 全局
      class UserModelView(ModelViewSet):
          queryset = userapp.User.objects.all()
          serializer_class = app03.UserSer
          ordering_fields = ("age", "hight")
      # 局部
      class UserModelView(ModelViewSet):
      queryset = userapp.User.objects.all()
      serializer_class = app03.UserSer
      filter_backends = [OrderingFilter]
    
    
      路由格式:http://127.0.0.1:8000/app03/user/?ordering=hight
    

    分页:

    settings.py:

      REST_FRAMEWORK = {
          # 分页配置
          'DEFAULT_PAGINATION_CLASS':
              'rest_framework.pagination.PageNumberPagination',
              'PAGE_SIZE': 3  # 每页的数据量
      }
    

    views.py:

      from rest_framework.viewsets import ModelViewSet
      from rest_framework.pagination import PageNumberPagination  # Django自带的分页
      class MyPage(PageNumberPagination):
          page_size_query_param = "max_page"
          page_query_param = "page"
    
    
      class UserModelView(ModelViewSet):
          queryset = userapp.User.objects.all()
          serializer_class = app03.UserSer
          pagination_class = MyPage
    
      路由格式:http://127.0.0.1:8000/app03/user/?max_page=3&page=1
  • 相关阅读:
    centos redis 安装 php-redis扩展安装 及使用
    mysql 大数据分页查询优化
    nginx https ssl 配置
    mysql 集群 数据同步
    linux 挂载U盘
    centos yum 没有可用软件包 nginx。
    nginx 负载均衡 反向代理
    nginx 配置
    mac 多php版本安装
    Foundation框架
  • 原文地址:https://www.cnblogs.com/kaka007/p/14025401.html
Copyright © 2020-2023  润新知