Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a Django Rest Framework Request object with data to a Django HttpRequest object?

I'm new to Django world and I'm trying to build a Django application with 2 function based views wherein one function/view should be able to call the other view. The reason I'm trying to do this is to reduce avoid writing the logic again which is available in my other API.

@api_view(['POST'])
def PerformActionOne(request):


    result_one = PerformActionTwo(request)
    result_two = DoSomethingElse(result_one)

    return Response(result_two)


@api_view(['POST'])
def PerformActionTwo(request):
    # Performs some calculation and returns rest_framework.response Response with some data in it
    # result is a dictionary 
    result = Calculate() 
    return Response(result) 

In the above code I'm getting an error in this line result_one = PerformActionTwo(request)

Error: Exception Type: AssertionError Exception Value:
The request argument must be an instance of django.http.HttpRequest, not rest_framework.request.Request.

I tried to look it up online and read documentation but I couldn't get a solution to this. I apologize if this is a duplicate question. Any leads on this will be greatly appreciated.

like image 809
Chandan Naik Avatar asked Sep 01 '25 20:09

Chandan Naik


1 Answers

not for certain on this but it's possible that the django.http.HttpRequest object is on the rest_framework.request.Request object under the _request param. Maybe try logging request._request to see what type of object it is?

like image 175
jkeary Avatar answered Sep 04 '25 15:09

jkeary