Reputation: 1604
Is it any possibility to get all actions names from ViewSet
in DRF?
I mean not only standard list
, retrieve
etc but custom ones too (defined by @action
decorator)
I've tried to use action_map
but it is empty
Upvotes: 3
Views: 2841
Reputation: 523
I'm using rest_framework version 3.11.2
and there you can do it like that:
actions = [action for action in YourViewsetClass.get_extra_actions()]
action_names = [action.__name__ for action in YourViewsetClass.get_extra_actions()]
action_url_names = [action.url_name for action in YourViewsetClass.get_extra_actions()]
Upvotes: 2
Reputation: 88549
I don't think there is a direct way to get all the actions specified in a ViewSet class. But, the Routers are usually generating the URLs in a similar way. So, I'm going to use the DRF Routers here,
from rest_framework.routers import SimpleRouter
router = SimpleRouter()
routes = router.get_routes(YourViewsetClass)
action_list = []
for route in routes:
action_list += list(route.mapping.values())
distinct_action_list = set(action_list)
Upvotes: 3