Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve viewset action docs #9638

Open
k0t3n opened this issue Jan 29, 2025 · 0 comments
Open

Improve viewset action docs #9638

k0t3n opened this issue Jan 29, 2025 · 0 comments

Comments

@k0t3n
Copy link

k0t3n commented Jan 29, 2025

Hi,
I encountered a problem when overriding viewset-level attributes doesn't work for actions if a route is configured as .as_view().

Example:

# views.py
class TestView(viewsets.ModelViewSet):
    @action(methods=['get'], detail=False, url_name='test_action', permission_classes=[IsAuthenticated,])
    def test_action(self, request):
        return Response()

# urls.py
urlpatterns = [
    path('test_urlpattern/', TestViewSet.as_view({'get': 'test_action'},)),
]

# tests.py
@pytest.mark.django_db
def test_case(client):
    url = reverse('test_urlpattern')
    response = client.get(url)
    assert response.status_code == 200  # expected 401!

You can override it using initkwargs only

# urls.py
urlpatterns = [
    # permission will be added
    path('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.

Image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant