Move some code from GenericAPIView post method to other methods in django

Question:

I have my django GenericAPIView with post method, I want some part of the logic which uses self to be in some other method and exccute that code when my post method gets hit

class MyAPIView(GenericAPIView):
    def post(self, request, *args, **kwargs):
        
Asked By: jimmy

||

Answers:

You can do something like this!

class MyAPIView(CreateAPIView):

def custom_function(self):
    """place your logic here!"""
    pass

def post(self, request, *args, **kwargs):
    # This will call our custom function as soon as post request is hit
    self.custom_function()
    return super().post(request, *kwargs, *args)