Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django generics.ListAPIView accepting POST method

I have a Django view which extends generics.ListAPIView. It works fine with get requests, however since the char limits of the URL, now I need to send the request via POST. It is the same request, the only thing I need to change is the method to POST. My current code is pretty simple:

class MyClass(generics.ListAPIView):
serializer_class = MySerializer
paginate_by = 1

def get_queryset(self):
    queryset = SomeClass.objects.all()
    # do some filtering

How could I add POST support to this class?

like image 262
jeanc Avatar asked Jan 27 '23 20:01

jeanc


1 Answers

Try this:

class MyClass(generics.ListAPIView):
    serializer_class = MySerializer
    paginate_by = 1

    def get_queryset(self):
        queryset = SomeClass.objects.all()
        # do some filtering


    def post(self, request, *args, **kwargs):
        return self.list(request, *args, **kwargs)
like image 149
MikeS Avatar answered Jan 31 '23 09:01

MikeS