1. [
  1. 课程介绍
  2. ](_index_)
    1. [
  1. 引入DjangoRESTframework
  2. ](c01-introducetodrf_index)
  3. -
  4. [
  5. Web应用模式
  6. ](c01-introducetodrf_separatedfrontendandbackend)
    1. [
  1. 认识RESTful
  2. ](c01-introducetodrf_introducetorest)
    1. [
  1. RESTful设计方法
  2. ](c01-introducetodrf_howtodesignrest)
    1. [
  1. 使用Django开发REST接口
  2. ](c01-introducetodrf_developrestapiwithdjango)
    1. [
  1. 明确REST接口开发的核心任务
  2. ](c01-introducetodrf_coretasktodeveloprestapi)
    1. [
  1. DjangoRESTframework简介
  2. ](c01-introducetodrf_aboutdrf)
    1. [
  1. DRF工程搭建
  2. ](c02-drfproject_index)
  3. -
  4. [
  5. 环境安装与配置
  6. ](c02-drfproject_installandconfig)
    1. [
  1. 见识DRF的魅力
  2. ](c02-drfproject_thefirstdrfprogram)
    1. [
  1. Serializer序列化器
  2. ](c03-serializer_index)
  3. -
  4. [
  5. 定义Serializer
  6. ](c03-serializer_declaring)
    1. [
  1. 序列化使用
  2. ](c03-serializer_serializing)
    1. [
  1. 反序列化使用
  2. ](c03-serializer_deserializing)
    1. [
  1. 模型类序列化器ModelSerializer
  2. ](c03-serializer_modelserializer)
    1. [
  1. 视图
  2. ](c04-view_index)
  3. -
  4. [
  5. RequestResponse
  6. ](c04-view_requestandresponse)
    1. [
  1. 视图概览
  2. ](c04-view_view)
    1. [
  1. 视图说明
  2. ](c04-view_viewintroduction)
    1. [
  1. 视图集ViewSet
  2. ](c04-view_viewset)
    1. [
  1. 路由Router
  2. ](c04-view_routers)
    1. [
  1. 其他功能
  2. ](c05-components_index)
  3. -
  4. [
  5. 认证
  6. ](c05-components_authentication)
    1. [
  1. 权限
  2. ](c05-components_permissions)
    1. [
  1. 限流
  2. ](c05-components_throttling)
    1. [
  1. 过滤
  2. ](c05-components_filtering)
    1. [
  1. 排序
  2. ](c05-components_ordering)
    1. [
  1. 分页
  2. ](c05-components_pagination)
    1. [
  1. 版本
  2. ](c05-components_versioning)
    1. [
  1. 异常处理
  2. ](c05-components_exceptions)
    1. [
  1. 自动生成接口文档
  2. ](c05-components_documents)
    1. [
    2. Published with GitBook
    3. ](https://www.gitbook.com)

课程介绍

限流Throttling

可以对接口访问的频次进行限制,以减轻服务器压力。

使用

可以在配置文件中,使用DEFAULT_THROTTLE_CLASSESDEFAULT_THROTTLE_RATES进行全局配置,

  1. REST_FRAMEWORK = {
  2. 'DEFAULT_THROTTLE_CLASSES': (
  3. 'rest_framework.throttling.AnonRateThrottle',
  4. 'rest_framework.throttling.UserRateThrottle'
  5. ),
  6. 'DEFAULT_THROTTLE_RATES': {
  7. 'anon': '100/day',
  8. 'user': '1000/day'
  9. }
  10. }

DEFAULT_THROTTLE_RATES 可以使用 second, minute, hourday来指明周期。
也可以在具体视图中通过throttle_classess属性来配置,如

  1. from rest_framework.throttling import UserRateThrottle
  2. from rest_framework.views import APIView
  3. class ExampleView(APIView):
  4. throttle_classes = (UserRateThrottle,)
  5. ...

可选限流类

1) AnonRateThrottle
限制所有匿名未认证用户,使用IP区分用户。
使用DEFAULT_THROTTLE_RATES['anon'] 来设置频次
2)UserRateThrottle
限制认证用户,使用User id 来区分。
使用DEFAULT_THROTTLE_RATES['user'] 来设置频次
3)ScopedRateThrottle
限制用户对于每个视图的访问频次,使用ip或user id。
例如:

  1. class ContactListView(APIView):
  2. throttle_scope = 'contacts'
  3. ...
  4. class ContactDetailView(APIView):
  5. throttle_scope = 'contacts'
  6. ...
  7. class UploadView(APIView):
  8. throttle_scope = 'uploads'
  9. ...
  1. REST_FRAMEWORK = {
  2. 'DEFAULT_THROTTLE_CLASSES': (
  3. 'rest_framework.throttling.ScopedRateThrottle',
  4. ),
  5. 'DEFAULT_THROTTLE_RATES': {
  6. 'contacts': '1000/day',
  7. 'uploads': '20/day'
  8. }
  9. }

实例

  1. from rest_framework.authentication import SessionAuthentication
  2. from rest_framework.permissions import IsAuthenticated
  3. from rest_framework.generics import RetrieveAPIView
  4. from rest_framework.throttling import UserRateThrottle
  5. class BookDetailView(RetrieveAPIView):
  6. queryset = BookInfo.objects.all()
  7. serializer_class = BookInfoSerializer
  8. authentication_classes = [SessionAuthentication]
  9. permission_classes = [IsAuthenticated]
  10. throttle_classes = (UserRateThrottle,)