You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# urls.pyurlpatterns= [
# permission will be addedpath('test_urlpattern/', TestViewSet.as_view({'get': 'test_action'}, permission_classes=[IsAuthenticated, ])),
]
According to the docs, it's not clear this feature works for Router configuration only. No exceptions, warnings, or even related github issues were found regarding this.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
isn't .as_view only work for class based views only? do we need this in viewsets?
If your question is whether the drf needs .as_view() in view sets, I believe yes, at least because lots of users' code may rely on it. My suggestion is to add some sort of validation or warning on passing arguments to .as_view(), since if you try to reroute GET requests to a specific function (action) the requests would just be passed to the default get() handler no matter what you configured before.
Doing this TestViewSet.as_view({'get': 'test_action'} in your urls.py bypasses competely this work, and instructs DRF to use the test_action function for all GET request on the viewset.
If you look at how the action decorator is implemented, it mostly sets some attributes on the decorated method, to be picked up by the router, it doesn't change its runtime behaviour by checking its input or transforming its output.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Activity
stale commentedon Apr 27, 2025
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
auvipy commentedon Apr 28, 2025
isn't .as_view only work for class based views only? do we need this in viewsets?
k0t3n commentedon Apr 28, 2025
If your question is whether the drf needs
.as_view()in view sets, I believe yes, at least because lots of users' code may rely on it. My suggestion is to add some sort of validation or warning on passing arguments to.as_view(), since if you try to reroute GET requests to a specific function (action) the requests would just be passed to the defaultget()handler no matter what you configured before.browniebroke commentedon Jul 6, 2025
Yes, I agree that we could make this a bit clearer. The ViewSets page mentions this
as_view()API:The last sentence sort of discourages using it this way, but it's there... Later, the same page introduces actions.
A short note telling to not use them together in the actions section wouldn't hurt, yes.
Some technical notes
Routers do some work to convert dynamic routes from actions into routes and pass the options from
kwargs:django-rest-framework/rest_framework/routers.py
Lines 212 to 224 in 5c07060
Doing this
TestViewSet.as_view({'get': 'test_action'}in yoururls.pybypasses competely this work, and instructs DRF to use thetest_actionfunction for all GET request on the viewset.If you look at how the
actiondecorator is implemented, it mostly sets some attributes on the decorated method, to be picked up by the router, it doesn't change its runtime behaviour by checking its input or transforming its output.stale commentedon Oct 18, 2025
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
mahdighadiriii commentedon Nov 11, 2025
I want to fix docs.
Add warning for .as_view() with @action. Fixes encode#9638