Skip to content

Improve viewset action docs #9638

Open
Open
@k0t3n

Description

@k0t3n

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions