-1

How can I make two views in a DRF Generic viewset use the same url_paths and url_names provided the make use of the same or different methods but different details value, eg must can both be Get methods but would have details=True and details=False on them

sample code for more clarity;

 @action(methods=["get"], detail=False, url_path="list-users", url_name="users")
    def get_users(self, request):
        # code here
 @action(methods=["get"], detail=True, url_path="users", url_name="users")
    def get_user(self, request, id):

        # code here

get_users work with this endpoint -> {{base_url}}/{{prefix}}/users

get_user does not work with -> {{base_url}}/{{prefix}}/{{user-id}}/users

but if I change the url_path and url_name to something else eg -> single-user

then the endpoint for getting a single user works -> {{base_url}}/{{prefix}}/{{user-id}}/single-user

how can I solve this to make the users and user have the same url_name and url_path because the actions have detail as False(users) and True(user) for both of them respectively

NB; Also please note that this viewset does not make use of any model

Onengiye Richard
  • 348
  • 6
  • 12

1 Answers1

0

So I figured this out, DRF actually has Routing additional HTTP methods for extra actions, which can be viewed here; https://www.django-rest-framework.org/api-guide/viewsets/#routing-additional-http-methods-for-extra-actions

Onengiye Richard
  • 348
  • 6
  • 12