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

I'd like to be able to add multiple @detail_route/@list_route handlers for the same URL #2838

Closed
andriy-s opened this issue Apr 18, 2015 · 1 comment

Comments

@andriy-s
Copy link

SIDE NOTE: I've read through CONTRIBUTING.md, and it suggests "it's a good idea to make pull requests early on". It also doesn't say it is mandatory to also create an issue, write to the mailing list, etc. Following the suggestions from CONTRIBUTING, I submitted a PR #2820, attaching a detailed comment with a description of what I'm trying to achieve. But having been ignored for 5 days already makes me think just submitting a PR is not actually enough, so I decided to created an accompanying issue (this issue) as well.

Back to the subject. Currently, if I need an ad hoc route to handle several HTTP methods I'm expected to define a single method in a view set, and then explicitly check for the value of the request.method to decide what code path to choose.

Example. Lets assume I have a UserViewSet similar to the one from the DRF's documentation. I'd like a single sub-URL to be used for both setting and resetting (deleting) the user's password. Currently, I can do this like follows:

class UserViewSet(ModelViewSet):
    ...

    @detail_route(methods=['post', 'delete'])
    def password(self, request, pk=None):
        if request.method == 'POST':
            # set password
            ...
        else:
            # reset (delete) password
            ...

This Pull Request allows to define several decorated viewset methods with the same url_path but different methods, and lets DRF route requests to these methods automatically based on both URL the HTTP method, just like with predefined list, get, update etc. viewset methods. In the client side code this will look like follows:

class UserViewSet(ModelViewSet):
    ...

    @detail_route(methods=['post'], url_path='password')
    def set_password(self, request, pk=None):
        # set password
        ...

    @detail_route(methods=['delete'], url_path='password')
    def reset_password(self, request, pk=None):
        # reset (delete) password
        ...

PR #2820 makes this possible in (as far as I can see) backward-compatible manner. If the proposed change is acceptable in general, I'll extend the PR with unit-tests.

Regards,
Andriy.

@tomchristie
Copy link
Member

Closing as per #2820.

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

2 participants