[Fixed]-Is it possible to add a new function to the class based view of django rest framework

1👍

Pass a type variable in your view, and use the available HTTP methods. Since it appears you are retreiving data, the GET method is usually used for that, but you can truthfully use whatever method you want. The GET method allows you to put your variables in the URL too, if that’s what you want.

Once you put the type variable in the request, you can just use if statements to determine what to do with it.

You could also create a different view for each type of request, although that might be overkill.

This is an example assuming use of the GET method:

class foo(APIView):
    # This uses the GET method.  POST, PUT, PATCH etc. would use
    # def post(...), def put(...)...
    def get(self, request, format=None):
        # Check what type of request is being made and return the proper response.
        if request.POST['type'] == 'get_user_by_name':
            jobseekers = JobSeeker.objects.filter(name=request.POST['name'])
            serializer = JobseekerSerializer(jobseekers, many=True)
            return Response(serializer.data)
        elif request.POST['type'] == 'get_user_by_email':
            jobseekers = JobSeeker.objects.filter(email=email)
            serializer = JobseekerSerializer(jobseekers, many=True)
            return Response(serializer.data)
        elif ...
👤PoDuck

Leave a comment