File tree Expand file tree Collapse file tree 3 files changed +12
-0
lines changed Expand file tree Collapse file tree 3 files changed +12
-0
lines changed Original file line number Diff line number Diff line change @@ -201,6 +201,7 @@ $ git clone https://github.com/HelloGitHub-Team/HelloDjango-REST-framework-tutor
201
201
12. [基于 drf-haystack 实现文章搜索接口](https://www.zmrenwu.com/courses/django-rest-framework-tutorial/materials/101/))
202
202
13. [加缓存为接口提速](https://www.zmrenwu.com/courses/django-rest-framework-tutorial/materials/102/))
203
203
14. [API 版本管理](https://www.zmrenwu.com/courses/django-rest-framework-tutorial/materials/103/))
204
+ 15. [限制接口访问频率](https://www.zmrenwu.com/courses/django-rest-framework-tutorial/materials/104/))
204
205
205
206
# # 公众号
206
207
< p align=" center" >
Original file line number Diff line number Diff line change 8
8
from rest_framework .permissions import AllowAny
9
9
from rest_framework .response import Response
10
10
from rest_framework .serializers import DateField
11
+ from rest_framework .throttling import AnonRateThrottle
11
12
from rest_framework_extensions .cache .decorators import cache_response
12
13
from rest_framework_extensions .key_constructor .bits import (
13
14
ListSqlQueryKeyBit ,
@@ -199,9 +200,14 @@ def get_queryset(self):
199
200
return Tag .objects .all ().order_by ("name" )
200
201
201
202
203
+ class PostSearchAnonRateThrottle (AnonRateThrottle ):
204
+ THROTTLE_RATES = {"anon" : "5/min" }
205
+
206
+
202
207
class PostSearchView (HaystackViewSet ):
203
208
index_models = [Post ]
204
209
serializer_class = PostHaystackSerializer
210
+ throttle_classes = [PostSearchAnonRateThrottle ]
205
211
206
212
207
213
class ApiVersionTestViewSet (viewsets .ViewSet ):
Original file line number Diff line number Diff line change 144
144
# API 版本控制
145
145
"DEFAULT_VERSIONING_CLASS" : "rest_framework.versioning.NamespaceVersioning" ,
146
146
"DEFAULT_VERSION" : "v1" ,
147
+ # 限流
148
+ "DEFAULT_THROTTLE_CLASSES" : [
149
+ "rest_framework.throttling.AnonRateThrottle" ,
150
+ ],
151
+ "DEFAULT_THROTTLE_RATES" : {"anon" : "10/min" },
147
152
}
You can’t perform that action at this time.
0 commit comments