Rest framework功能的配置

认证Authentication

方法:
第一种:
REST_FRAMEWORK = {
‘DEFAULT_AUTHENTICATION_CLASSES’: (
‘rest_framework.authentication.BasicAuthentication’, # 基本认证
‘rest_framework.authentication.SessionAuthentication’, # session认证
)
}
第二种:
在需要的视图中进行添加认证
from rest_framework.authentication import SessionAuthentication, BasicAuthentication
from rest_framework.views import APIView
class ExampleView(APIView):
authentication_classes = (SessionAuthentication, BasicAuthentication)

权限Permissions

方法:
第一种:
可以在配置文件中设置默认的权限管理类,如
REST_FRAMEWORK = {
‘DEFAULT_PERMISSION_CLASSES’: (
‘rest_framework.permissions.IsAuthenticated’,
)
}
如果未指明,则采用如下默认配置
‘DEFAULT_PERMISSION_CLASSES’: (
‘rest_framework.permissions.AllowAny’,
)
第二种:
在需要的视图中进行添加权限
from rest_framework.permissions import IsAuthenticated
from rest_framework.views import APIView
class ExampleView(APIView):
permission_classes = (IsAuthenticated,)

限流Throttling

方法:
第一种:
REST_FRAMEWORK = {
‘DEFAULT_THROTTLE_CLASSES’: (
‘rest_framework.throttling.AnonRateThrottle’,
‘rest_framework.throttling.UserRateThrottle’
),
‘DEFAULT_THROTTLE_RATES’: {
‘anon’: ‘100/day’,
‘user’: ‘1000/day’
}
}
第二种:
在需要的视图中进行添加限流
from rest_framework.throttling import UserRateThrottle
from rest_framework.views import APIView
class ExampleView(APIView):
throttle_classes = (UserRateThrottle,)

过滤Filtering

方法:
先安装扩展包:pip insall django-filter
第一种:
INSTALLED_APPS = [

‘django_filters’, # 需要注册应用,
]

REST_FRAMEWORK = {
‘DEFAULT_FILTER_BACKENDS’: (‘django_filters.rest_framework.DjangoFilterBackend’,)
}
第二种:
在视图中添加过滤属性
class BookListView(ListAPIView):
queryset = BookInfo.objects.all()
serializer_class = BookInfoSerializer
filter_fields = (‘btitle’, ‘bread’)

排序

在视图中直接设置:
class BookListView(ListAPIView):
queryset = BookInfo.objects.all()
serializer_class = BookInfoSerializer
filter_backends = [OrderingFilter]
ordering_fields = (‘id’, ‘bread’, ‘bpub_date’)

分页Pagination

方法:
第一种:
REST_FRAMEWORK = {
‘DEFAULT_PAGINATION_CLASS’: ‘rest_framework.pagination.PageNumberPagination’,
‘PAGE_SIZE’: 100 # 每页数目
}
注意:如果在视图内关闭分页功能,只需在视图内设置
pagination_class = None
第二种:
也可通过自定义Pagination类,来为视图添加不同分页行为。在视图中通过pagination_clas属性来指明。
class LargeResultsSetPagination(PageNumberPagination):
page_size = 1000
page_size_query_param = ‘page_size’
max_page_size = 10000
class BookDetailView(RetrieveAPIView):
queryset = BookInfo.objects.all()
serializer_class = BookInfoSerializer
pagination_class = LargeResultsSetPagination

异常处理 Exceptions

REST framework提供了异常处理,我们可以自定义异常处理函数。
from rest_framework.views import exception_handler
def custom_exception_handler(exc, context):
# 先调用REST framework默认的异常处理方法获得标准错误响应对象
response = exception_handler(exc, context)

# 在此处补充自定义的异常处理
if response is not None:
    response.data['status_code'] = response.status_code

return response

在配置文件中声明自定义的异常处理
REST_FRAMEWORK = {
‘EXCEPTION_HANDLER’: ‘my_project.my_app.utils.custom_exception_handler’
}
如果未声明,会采用默认的方式,如下
REST_FRAMEWORK = {
‘EXCEPTION_HANDLER’: ‘rest_framework.views.exception_handler’
}

自动生成接口文档

方法:
安装依赖:pip install coreapi
设置接口文档访问路径:
在总路由中添加接口文档路径。
文档路由对应的视图配置为rest_framework.documentation.include_docs_urls,
参数title为接口文档网站的标题。
from rest_framework.documentation import include_docs_urls
urlpatterns = [

url(r’^docs/’, include_docs_urls(title=’My API title’))
]
3. 文档描述说明的定义位置
1) 单一方法的视图,可直接使用类视图的文档字符串,如

class BookListView(generics.ListAPIView):
“””
返回所有图书信息.
“””
2)包含多个方法的视图,在类视图的文档字符串中,分开方法定义,如

class BookListCreateView(generics.ListCreateAPIView):
“””
get:
返回所有图书信息.

post:
新建图书.
"""

3)对于视图集ViewSet,仍在类视图的文档字符串中分开定义,但是应使用action名称区分,如

class BookInfoViewSet(mixins.ListModelMixin, mixins.RetrieveModelMixin, GenericViewSet):
“””
list:
返回图书列表数据

retrieve:
返回图书详情数据

latest:
返回最新的图书数据

read:
修改图书的阅读量
"""
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值