REST_FRAMEWORK={
'DEFAULT_AUTHENTICATION_CLASSES':( #drf认证
'opt.authentication.CustomAuthentication',
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.BasicAuthentication'
),
'DEFAULT_PERMISSION_CLASSES':( #drf权限设置
'rest_framework.permissions.IsAuthenticatedOrReadOnly',
#'opt.permissions.IsXiaoMingRermission',
),
# 'DEFAULT_THROTTLE_CLASSES':[ #drf流量设置
# 'rest_framework.throttling.AnonRateThrottle',
# 'rest_framework.throttling.UserRateThrottle',
# 'rest_framework.throttling.ScopedRateThrottle'
# ],
# 'DEFAULT_THROTTLE_RATES':{ #drf具体配置
# 'anon':"2/day",
# 'user':"5/day",
# 'vip':'3/min'
# },
# 'DEFAULT_FILTER_BACKENDS':[ #drf 过滤器
# 'django_filters.rest_framework.DjangoFilterBackend', #过滤
# 'rest_framework.filters.OrderingFilter' #排序
# ],
# 'DEFAULT_PAGINATION_CLASS':'rest_framework.pagination.LimitOffsetPagination',#偏移量分页器
# 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination', # 页码分页器
#
# 'PAGE_SIZE':5,
#"EXCEPTION_HANDLER":('opt.exceptions.custom_exception_handler') #自定义异常处理
}
drf认证
全局配置
'DEFAULT_AUTHENTICATION_CLASSES':( #drf认证
'opt.authentication.CustomAuthentication',
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.BasicAuthentication'
)
局部配置
from rest_framework.authentication import SessionAuthentication,BasicAuthentication
class ExampleView(APIView):
authentication_classes = [SessionAuthentication,BasicAuthentication]
自定义Custom认证
from rest_framework.authentication import SessionAuthentication,BasicAuthentication
from django.contrib.auth import get_user_model
class CustomAuthentication(BasicAuthentication):
def authenticate(self, request):
user=request.query_params.get("user")
user=get_user_model().objects.filter(username=user).first()
return (user,None)
drf权限设置
全局配置
'DEFAULT_PERMISSION_CLASSES':( #drf权限设置
'rest_framework.permissions.IsAuthenticatedOrReadOnly',
#'opt.permissions.IsXiaoMingRermission',
),
局部配置
from rest_framework.permissions import IsAuthenticated,IsAdminUser,IsAuthenticatedOrReadOnly
from .permissions import IsXiaoMingRermission
class HomeInfoAPIView(APIView):
permission_classes=[IsAuthenticated]#只能被已经登录的用户访问
#permission_classes = [IsAdminUser] #只能被已经登录的管理员访问
#permission_classes = [IsAuthenticatedOrReadOnly] # 用户可读,登录可改
#permission_classes=[IsXiaoMingRermission]
自定义权限
from rest_framework.permissions import BasePermission
class IsXiaoMingRermission(BasePermission):
def has_permission(self, request, view):
#结果为True则表示可以访问
return bool(request.user and request.user.username=='zzz')
drf限流
全局配置
'DEFAULT_THROTTLE_CLASSES':[ #drf流量限置
'rest_framework.throttling.AnonRateThrottle',
'rest_framework.throttling.UserRateThrottle',
'rest_framework.throttling.ScopedRateThrottle'
],
'DEFAULT_THROTTLE_RATES':{ #drf具体配置
'anon':"2/day",
'user':"5/day",
'vip':'3/min' #自定义vip
},
局部配置
from rest_framework.throttling import UserRateThrottle
class ControlAPIView(APIView):
throttle_classes = [UserRateThrottle]
throttle_scope='vip'
drf过滤排序
pip install django-filter
局部配置
filter_backends = [DjangoFilterBackend,OrderingFilter] #局部配置过滤和排序 过滤和排序必须同时使用
filterset_fields = ['name', 'sex']
ordering_fields=['id','age','classmeta']
全局配置
# 'DEFAULT_FILTER_BACKENDS':[ #drf 过滤器
# 'django_filters.rest_framework.DjangoFilterBackend', #过滤
# 'rest_framework.filters.OrderingFilter' #排序
# ],
使用方式
127.0.0.1:8000/api4/filter/?name=2lly
127.0.0.1:8000/api4/filter/?name=2lly&ordering=-id
drf分页
全局配置
# 'DEFAULT_PAGINATION_CLASS':'rest_framework.pagination.LimitOffsetPagination',#偏移量分页器
# 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination', # 页码分页器
#
局部配置
from rest_framework.pagination import LimitOffsetPagination,PageNumberPagination
from .paging import StudentPage
class PagingAPIView(ListCreateAPIView):
pagination_class = StudentPage
自定义分页
from rest_framework.pagination import PageNumberPagination
class StudentPage(PageNumberPagination):
page_query_param = 'page'
page_size_query_param = 'size'
page_size = 2
max_page_size = 4
使用方式
127.0.0.1:8000/api4/page/?page=2
127.0.0.1:8000/api4/page/?offset=5&page=2
drf异常处理
from rest_framework.views import exception_handler as drf_exception_handler
from rest_framework.response import Response
from django.db import DataError
def custom_exception_handler(exc,context):
response=drf_exception_handler(exc,context)
if response is None:
if isinstance(exc,ZeroDivisionError):
response=Response({'msg':'0不能作为除数'})
if isinstance(exc,DataError):
response = Response({'msg': '数据异常'})
return response
REST_FRAMEWORK={
...
"EXCEPTION_HANDLER":('opt.exceptions.custom_exception_handler')
}
drf接口文件
pip install coreapi
pip install drf-yasg