Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get data from post request in generics.ListAPIView DRF?

There is some view:

class Search(CategoryDetail):
serializer_class = ProductSerializer

def get_queryset(self):
    # print(self.request)
    return self.get_filters(None)

It inherits from another ListAPIView and does something.

From front on Vue.js throught post i pass some query parameter:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

async Search(){
        axios
            .post('api/products/search', {'query': this.query})
            .then(response =>{
                this.products = response.data
            })
            .catch(error=>{
                console.log(error)
            })
    }

Initially, i performed work with this post request through the search view function:

@api_view(['POST'])
def search(request):
   query = request.data.get('query')

   if query:
       products = Product.objects.filter(Q(name__icontains=query) | Q(description__icontains = query))
       serializer = ProductSerializer(products, many=True)
       return Response(serializer.data)
   else:
       return Response({"products": []})

But now i need to change the view function to a class, but in the class i need to get the same query = request.data.get(‘query’), however, when i try, the Bad Request error is constantly returned.

>Solution :

ListAPIView doesn’t accept post requests. You should pass your query as a GET request query parameter and reach it in the get_queryset method to filter your results.

class ProductList(generics.ListCreateAPIView):
    serializer_class = ProductSerializer

    def get_queryset(self):
        query = self.request.query_params.get('query')
        if query is not None:
            return Product.objects.filter(Q(name__icontains=query) | Q(description__icontains = query))
        else:
            Product.objects.none()

If you must use POST data and return the list of objects, you can implement your own APIView and use its post method.

class PrdocutList(APIView):
      def post(self, request, format=None):
        query = request.data.get('query')
        if query:
            products = Product.objects.filter(Q(name__icontains=query) | Q(description__icontains = query))
        else:
            products =  Product.objects.none()
        serializer = ProductSerializer(products, many=True)
        return Response(serializer.data)

You can also implement your own filter_backend.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading