基于函数的视图上的 Django Rest Framework 范围限制

Django Rest Framework Scope Throttling on function based view

想问问是否有人知道如何在基于函数的视图中为不同的请求方法设置不同的限制范围。

例如

@api_view(['GET', 'POST'])
def someFunction(request):
    if request.method == 'GET':
          # set scope for get requests
    elif request.method == 'POST':
          # set scope for post requests

我试着环顾四周,但所有答案仅针对基于 class 的视图。感谢您的帮助,谢谢。

您可以通过首先创建所有自定义节流来解决此问题 类。注意:类 中只有油门,但视图是函数。

class PostAnononymousRateThrottle(throttling.AnonRateThrottle):
    scope = 'post_anon'
    def allow_request(self, request, view):
        if request.method == "GET":
            return True
        return super().allow_request(request, view)

class GetAnononymousRateThrottle(throttling.AnonRateThrottle):
    scope = 'get_anon'
    def allow_request(self, request, view):
        if request.method == "POST":
            return True
        return super().allow_request(request, view)

class PostUserRateThrottle(throttling.UserRateThrottle):
    scope = 'post_user'
    def allow_request(self, request, view):
        if request.method == "GET":
            return True
        return super().allow_request(request, view)

class GetUserRateThrottle(throttling.UserRateThrottle):
    scope = 'get_user'
    def allow_request(self, request, view):
        if request.method == "POST":
            return True
        return super().allow_request(request, view)

如果您不需要身份验证或方法类型,您可以选择消除 类。

然后你需要导入这个

from rest_framework.decorators import api_view, throttle_classes

然后你可以用throttle_classes装饰器包装你的函数视图并创建所有权限

@api_view(['GET', 'POST'])
@throttle_classes([PostAnononymousRateThrottle, GetAnononymousRateThrottle, PostUserRateThrottle, GetUserRateThrottle])
def someFunction(request):
    if request.method == 'POST':
        return Response({"message": "Got some data!", "data": request.data})
    elif request.method == 'GET':
        return Response({"message": "Hello, world!"})

别忘了在 settings.py

中提及节流率
REST_FRAMEWORK = {
    'DEFAULT_THROTTLE_RATES': {
        'post_anon': '3/minute',
        'get_anon': '1/minute',
        'post_user': '2/minute',
        'get_user': '2/minute'
    }
}

参考:https://medium.com/analytics-vidhya/throttling-requests-with-django-rest-framework-for-different-http-methods-3ab0461044c

我终于找到了基于函数的视图的解决方法。 这是我的实现方式。

正如之前的回答中所解释的那样,我们需要根据需要扩展 UserRateThrottle class 或 AnonRateThrottle class。

就我而言,我更感兴趣的是限制用户的请求。

from rest_framework.throttling import UserRateThrottle

class CustomThrottle(UserRateThrottle):
     scope = 'my_custom_scope'
     def allow_request(self, request, view):
         if request.method == 'GET':
            self.scope = 'get_scope'
            self.rate = '2/hour'
            return True
         return super().allow_request(request, view)

并在设置中:

'DEFAULT_THROTTLE_RATES': {
        'my_custom_scope': '3/day'
}

默认情况下,此 class 将根据设置文件中设置的速率限制 POST 请求。我在此处添加的内容是在请求方法为 GET 的情况下更改范围和速率。如果不进行此更改,由于 DRF Throttlers 使用的默认缓存,可能会出现一些问题。我们需要在 CustomThrottle class 本身内部设置速率和范围,否则与 POST 方法相关的范围将同时应用于 GET 和 POST.

最后,我们在 function-based 视图上添加装饰器。

from rest_framework import api_view, throttle_classes
import CustomThrottle

@api_view(['GET', 'POST'])
@throttle_classes([CustomThrottle])
def someFunction(request):
    if request.method == 'GET':
          # set scope for get requests
    elif request.method == 'POST':
          # set scope for post requests

就是这样 :D