ModelViewSet 分页 过滤

ModelViewSet 分页 过滤

局部使用

分页

  导包

from django_filters.rest_framework import DjangoFilterBackend

  类调用

class MyPage(PageNumberPagination):
    page_size_query_param = 'max_page'
    page_query_param = 'page'
class UserViewSet(viewsets.ModelViewSet):
  
  queryset = User.objects.all()
  # 分页   pagination_class
= MyPage   serializer_class = UserSerializer #展示序列化器所有信息

过滤

  导包

from rest_framework import filters

  调用

class UserViewSet(viewsets.ModelViewSet):
  
  queryset = User.objects.all()  
  filter_backends = (DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter)   
  filter_class = GoodsFilters

  serializer_class = UserSerializer #展示序列化器所有信息

 

全局使用

REST_FRAMEWORK ={
    'DEFAULT_THROTTLE_RATES': {
        'user': '100/hour', # 认证用户每小时10次
        'anon': '1000/day',
    },
    # 过滤全局配置  (ModelViewSet)
    'DEFAULT_FILTER_BACKENDS':(
        'django_filters.rest_framework.DjangoFilterBackend',
        'rest_framework.filters.OrderingFilter'
    ),
    # 分页
    'DEFAULT_PAGINATION_CLASS':
        'rest_framework.pagination.PageNumberPagination',
        'PAGE_SIZE': 2,# 每⻚数⽬
    # 身份认证
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
        'rest_framework.authentication.SessionAuthentication',
        # 登陆权限
        'rest_framework.authentication.BasicAuthentication',
    ),
    # #全局配置JWT验证设置
    # 'DEFAULT_PERMISSION_CLASSES': (
    #     'rest_framework.permissions.IsAuthenticated',
    # ),
}

 

posted @ 2020-12-03 15:36  就叫我老李吧  阅读(262)  评论(0编辑  收藏  举报