I work with DRF to create an API, and I used a master class to do some validation for my class-based views:
class MasterClass(APIView): def dispatch(self, request, *args, ** response = super(FaveoAPIView, self).dispatch(request, *args, **kwargs) # I call super because I need access to request data. # <some validations here> # Return a JsonResponse with an error message if validations fails class MyView(MasteClass): def post(self, request, *args, **kwargs): # At this point request is: <WSGIRequest: POST '/api/path/'> # some DB transaction # ...
Checks fail with at least one error, but the database transaction is executed, I get an error message from the dispatch method, but the post method is executed before dispatch , I use breakpoints to view the stream, and this goes into the post method, and then to the dispatch method, for example, if they were separated by threads.
From docs :
The as_view entry point instantiates your class and calls its dispatch method (). the mailing list considers the request to determine whether it is GET, POST, etc., and passes the request for compliance to the method, if it is defined, or raises the HttpResponseNotAllowed, if not.
So, I thought that if I return an error response when sending, the post method should not be executed, why is it running? What am I doing wrong here?