Skip to content
Advertisement

url path is matching the wrong view in drf viewsets

Django beginner here,

I have two similar endpoints as shown below, they differ in their url_path of the action decorator and the request parameters requester_id and approval id

The problem

both /workflow_approvals/{requester_id}/ and /workflow_approvals/{approval_id}/ are routing to the requester method view(the first below)

JavaScript
JavaScript

my urls.py file looks like this

JavaScript

Advertisement

Answer

Django doesn’t have a way of differentiating /workflow_approvals/{requester_id}/ and /workflow_approvals/{approval_id}/, since the regex pattern for (?P<requester_id>[^/.]+) matches both. So when you make the request with either a requester_id or approval_id, it’ll always end up routing to the former.

You will need instead differentiate the url_paths somehow so they don’t collide. For instance, including an extra string in the path, like matching url_path='requester/(?P<requester_id>[^/.]+)') and url_path='approval/(?P<approval_id>w+)'). (You may want to check out the Routing for extra actions example in the docs.)

But possibly better yet, since it seems you’re trying to filter your list results based on parameters, I’d recommend checking out the Filtering options in Django Rest Framework. You could do something like:

JavaScript

and then be able to query via the standard list route, with query params like ?id=123 or ?requester_id=42.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement