How to mark a DRF ViewSet action as being exempt from the application of a custom middleware? #9462
Unanswered
browser-bug
asked this question in
Question & Answer
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I've created a Custom Django Middleware and added to the
MIDDLEWARE
settings variable correctly.Since this is applied to all DRF ViewSets by default, I would like to exempt some actions that don't need this check. The idea would be to check a flag inside the
process_view
function taking inspiration from the DjangoCsrfViewMiddleware
which checks if thecsrf_exempt
variable has been set by thecsrf_exempt
decorator. So I modified the custom middleware and created a custom decorator to exempt views explicitly.Having this I do something like this and it correctly enters the custom decorator before going inside the Django middleware.
So far so good until I noticed that the
view_func
of the custom middlewareprocess_view
doesn't correspond to the action function (which is being decorated) but to the ViewSet function.Inside the decorator:
view_func = <function MyViewSet.my_action at 0x79ee9c471760>
Inside the middleware:
view_func = <function MyViewSet at 0x79ee9c49c220>
Apparently, Django middlewares are applied at viewset level instead of action level. As a consequence
view_func
doesn't have thesome_condition
attribute set.Is there a way to decorate a viewset action and change the viewset level function or an alternative way to achieve what I'm trying to do?
Beta Was this translation helpful? Give feedback.
All reactions