How I can throttle only the POST method in this views

783 Views Asked by At
class ExampleView(APIView):
    throttle_classes = [UserRateThrottle]

    def get(self, request, format=None):
        content = {
            'status': 'request was permitted'
        }
        return Response(content)

    def post(self, request, format=None):
        content = {
            'status': 'request was permitted'
        }
        return Response(content)
   

I want to throttle only the post method view not the get. I have declared two views POST and GET and I want only to implement throttling for my POST view

3

There are 3 best solutions below

2
Siva On

You can try this, this overrides the get_throttles method and does not include throttles for POST method.

class ExampleView(APIView):
    throttle_classes = [UserRateThrottle]

    def get_throttles(self):
        if self.request.method == 'POST':
            return []
        return super().get_throttles()

    def get(self, request, format=None):
        content = {
            'status': 'request was permitted'
        }
        return Response(content)

    def post(self, request, format=None):
        content = {
            'status': 'request was permitted'
        }
        return Response(content)
1
Mahboob Nur On

You can try like this

from rest_framework.decorators import throttle_classes
from rest_framework.throttling import UserRateThrottle

class ExampleView(APIView):

    @throttle_classes([UserRateThrottle])
    def get(self, request, format=None):
        content = {
            'status': 'request was permitted'
        }
        return Response(content)

    def post(self, request, format=None):
        content = {
            'status': 'request was permitted'
        }
        return Response(content)
0
Mohammad Golam Dostogir On

By default, throttle_classes throttles all methods. If you want to limit it to only throttle the POST method in your Django view you need to change the get_throttles method by modifying get_throttles, you can choose which methods to throttle.

Try this:

from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.throttling import UserRateThrottle

class ExampleView(APIView):
    # No need to declare throttle_classes here

    def get_throttles(self):
        if self.request.method == 'POST':
            # Apply throttling only for POST requests
            return [UserRateThrottle()]
        return []  # Return an empty list for other methods

    def get(self, request, format=None):
        content = {
            'status': 'GET request was permitted'
        }
        return Response(content)

    def post(self, request, format=None):
        content = {
            'status': 'POST request was permitted'
        }
        return Response(content)

Here the get_throttles method is overridden to apply the UserRateThrottle only to POST requests. It returns an empty list for GET requests, meaning no throttling will be applied for this request and it will remain unaffected.