Home > Back-end >  Django Modelviewset try to create custom route
Django Modelviewset try to create custom route

Time:10-07

@action(detail=False, methods=['GET'], name='Get Vesting Locaitons')
def get_vesting_locations(self, request, pk=None, *args, **kwargs):

I'm trying to return json response and get 404 error

this is the the route regiser

router.register(r'vesting', VestingViewSet, basename='vesting')

and those are the urls im trying to get

http://localhost:8000/vesting/get_vesting_locations/617b8bd8-6fdd-43eb-948a-4b17d1a0a089/
http://localhost:8000/vesting/617b8bd8-6fdd-43eb-948a-4b17d1a0a089/get_vesting_locations/

CodePudding user response:

Defining the action with detail=False will tell the url that this view does not work on a single object. So it will build this url:

vesting/get_vesting_locations/

So trying to go to:

vesting/617b8bd8-6fdd-43eb-948a-4b17d1a0a089/get_vesting_locations/

will then give you a 404.

In order for this action to work with a single object and support the above url, set detail=True:

@action(detail=True, methods=['GET'], name='Get Vesting Locaitons')
def get_vesting_locations(self, request, pk=None, *args, **kwargs):
  • Related