Arvind Kumar
Arvind Kumar

Reputation: 973

Returning Response directly from an internal function in django rest framework

I have a view that calls the other functions. Now based on different scenarios, the function has to return some error messages to the user. But if use return in that internal function, view always returns a "200 OK" until an exception has been raised. Below is a sample code -

class BranchViewSet(BaseViewSet):
    serializer_class = BranchSerializer

    def create(self, request, *args, **kwargs):
        branch = super().create(request, *args, **kwargs)
        createBranchAdmin(branch.data)
        response = Response(branch.data, status=status.HTTP_201_CREATED)
        return response


def createBranchAdmin(data):
    schema = Branch.objects.filter(BranchId=data['BranchId'])[0]
    name = schema.contactPerson
    phone = schema.phoneNumber
    existingUser = User.objects.filter(phone=phone)

    if existingUser:
        response = Response("User Already Exists!", status=status.HTTP_400_BAD_REQUEST)
        return response

If a user already exists, I want to send this 400 Error to the user, but I get 201, even when the user exists. How to send this response to user directly from here?

Edit - I understand the point @iceagebaby is conveying. But this is a simple scenario, I have put forward. Sometimes there are multiple return points in a function, each carrying a different message and error. So, returning None would not help. If I create a new return variable for each and check it in the original view, in that case, the view becomes quite messy and there is no benefit of writing a new function.

Upvotes: 0

Views: 975

Answers (1)

iceagebaby
iceagebaby

Reputation: 76

You aren't returning the value of createBranchAdmin, so when you return the response it is always coming from response = Response(branch.data, status=status.HTTP_201_CREATED)

You should be either setting the return value of createBranchAdmin to a variable (returning None if there is no user or the 400 response if there is) then checking if the return of createBranchAdmin is None and returning accordingly or you could just throw an error and call that function in a try catch block and returning with the correct response (although I'm pretty sure this is a bit slower).

i.e. try something like this

class BranchViewSet(BaseViewSet):
    serializer_class = BranchSerializer

    def create(self, request, *args, **kwargs):
        branch = super().create(request, *args, **kwargs)
        ret = createBranchAdmin(branch.data)
        if ret is not None: return ret
        response = Response(branch.data, status=status.HTTP_201_CREATED)
        return response


def createBranchAdmin(data):
    schema = Branch.objects.filter(BranchId=data['BranchId'])[0]
    name = schema.contactPerson
    phone = schema.phoneNumber
    existingUser = User.objects.filter(phone=phone)

    if existingUser:
        response = Response("User Already Exists!", status=status.HTTP_400_BAD_REQUEST)
        return response
    return None

Upvotes: 1

Related Questions