Home > Software design >  DRF return JsonResponse from another function
DRF return JsonResponse from another function

Time:09-23

I am trying to return the JsonResponse from another function but I get the following error from DRF:

AssertionError: Expected a Response, HttpResponse or HttpStreamingResponse to be returned from the view, but received a <class 'NoneType'>

Here is my code

class SignedURL(GenericViewSet):
    queryset = fileUpload.objects.all()
    serializer_class = fileUploadSerializer

    def create(self, request, *args, **kwargs):

        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        function1(request)
        function2(request)
        function3(request) <-- must quit here if this function has an error and return the JSONresponse of this function.
        function4(request)


def function1(request):

        return JsonResponse(
            {
                "message": "Pass",
            },
            status=status.HTTP_400_BAD_REQUEST,

How can I return the JsonResponse from another function without writing return JsonResponse in the def create itself?

Further clarification:

I have ten different functions with ten different JsonResponse.

ie, function2,function3...function10. If, for example, function 4 fails, I would like to return the JsonResponse immediately from that function and not proceed further with the other functions after function 4 within the create call.

CodePudding user response:

Your function1 currently only returns a JsonResponse without any error checking. This will cause the script to stop and no further code being executed.

It will not proceed to any other function calls within the create method.

Your code in its current state should have the effect you are looking for. Have you imported JsonResponse?

CodePudding user response:

In general it sounds a bit anorthodox what you are trying to implement. In any case, you can capture the response of all functions and return it from the create method.

But in any case, you need some error handling, either inside your function (most likely, since you want to return a JSONResponse) or in the create method (e.g. using try/except) which seems it's not close to what you want to achieve, since you won't have something returned from each function that fails (unless you handle exceptions in EACH function).

So maybe something like:

class SignedURL(GenericViewSet):
    queryset = fileUpload.objects.all()
    serializer_class = fileUploadSerializer

    def create(self, request, *args, **kwargs):

        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        result1, is_success1 = function1(request)
        if is_success1 is False:
            # Quit here if this function has an error and return the JSONresponse of this function
            return result1

        # similar checks below
        result2, is_success2 = function2(request)
        result3, is_success3 = function3(request)
        ...

And

def function1(request):
    try:
        return 1 / 0, True
    except:
        return JsonResponse(
            {
                "message": "ERROR HAPPENED!"
            },
            status=status.HTTP_400_BAD_REQUEST
        ), False
  • Related