Skip to content

Multiples @detail_route with the same url_path #4264

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions rest_framework/routers.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ def get_urls(self):
"""
Use the registered viewsets to generate a list of URL patterns.
"""
ret = []
urls_data = {}

for prefix, viewset, basename in self.registry:
lookup = self.get_lookup_regex(viewset)
Expand All @@ -260,7 +260,31 @@ def get_urls(self):

view = viewset.as_view(mapping, **route.initkwargs)
name = route.name.format(basename=basename)
ret.append(url(regex, view, name=name))

# It is possible to define multiples '@detail_route' for the
# same endpoint with different methods (GET, POST, etc). So, we
# need to merge all those routes with the same URL allowing
# those multiples method
if regex in urls_data:
# merge mapping and view for the same regex
urls_data[regex]['mapping'].update(mapping)
else:
urls_data[regex] = {
'mapping': mapping,
'viewset': viewset,
'route': route,
'name': name,
}

ret = []
# Build all the URLs for the values saved on the previous iteration
for regex, values in urls_data.items():
name = values['name']
view = values['viewset'].as_view(
values['mapping'],
**values['route'].initkwargs
)
ret.append(url(regex, view, name=name))

return ret

Expand Down