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

Multiple detail_route for the same url_path #2708

Closed
cancan101 opened this issue Mar 18, 2015 · 3 comments
Closed

Multiple detail_route for the same url_path #2708

cancan101 opened this issue Mar 18, 2015 · 3 comments

Comments

@cancan101
Copy link
Contributor

I would like to have multiple detail_route for the same url_path but with different HTTP methods. I would like to do this so that I may specify different permission_classes.

    @detail_route(methods=['post'], url_path='users', permission_classes=[A])
    def users_create(self, request, **kwargs):
        data = request.data
        serializer = UserSerializer(data=data)
        serializer.is_valid(raise_exception=True)

        user = serializer.validated_data

        return Response(serializer.data, status=status.HTTP_201_CREATED)

    @detail_route(methods=['get'], url_path='users', permission_classes=[B])
    def users_list(self, request, **kwargs):
        serializer = UserSerializer(data=[], many=True)
        serializer.is_valid(raise_exception=True)
        return Response(serializer.data, status=status.HTTP_200_OK)

Unfortunately this does not seem to work in the browseable API:

HTTP 405 METHOD NOT ALLOWED
Content-Type: application/json
Vary: Accept
Allow: POST, OPTIONS

{
    "detail": "Method \"GET\" not allowed."
}
@carltongibson
Copy link
Collaborator

This is a usage question that you should really post on the mailing list...

Nonetheless, override APIView.get_permissions to inspect the request and return the appropriate permissions for the method.

@cancan101
Copy link
Contributor Author

Forgetting for a moment even wanting to have different permission classes, why is this not supported (and I don't think this is a usage question):

    @detail_route(methods=['post'], url_path='users')
    def users_create(self, request, **kwargs):
        data = request.data
        serializer = UserSerializer(data=data)
        serializer.is_valid(raise_exception=True)

        user = serializer.validated_data

        return Response(serializer.data, status=status.HTTP_201_CREATED)

    @detail_route(methods=['get'], url_path='users')
    def users_list(self, request, **kwargs):
        serializer = UserSerializer(data=[], many=True)
        serializer.is_valid(raise_exception=True)
        return Response(serializer.data, status=status.HTTP_200_OK)

@jpadilla
Copy link
Member

@cancan101 that request seems similar to what #2820 is trying to accomplish.

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

3 participants