1111
1212from rest_framework import permissions ,serializers ,viewsets
1313from rest_framework .compat import get_regex_pattern
14- from rest_framework .decorators import detail_route , list_route
14+ from rest_framework .decorators import action
1515from rest_framework .response import Response
1616from rest_framework .routers import DefaultRouter ,SimpleRouter
1717from rest_framework .test import APIRequestFactory
@@ -67,12 +67,12 @@ def get_object(self, *args, **kwargs):
6767
6868
6969class RegexUrlPathViewSet (viewsets .ViewSet ):
70- @list_route ( url_path = 'list/(?P<kwarg>[0-9]{4})' )
70+ @action ( detail = False , url_path = 'list/(?P<kwarg>[0-9]{4})' )
7171def regex_url_path_list (self ,request ,* args ,** kwargs ):
7272kwarg = self .kwargs .get ('kwarg' ,'' )
7373return Response ({'kwarg' :kwarg })
7474
75- @detail_route ( url_path = 'detail/(?P<kwarg>[0-9]{4})' )
75+ @action ( detail = True , url_path = 'detail/(?P<kwarg>[0-9]{4})' )
7676def regex_url_path_detail (self ,request ,* args ,** kwargs ):
7777pk = self .kwargs .get ('pk' ,'' )
7878kwarg = self .kwargs .get ('kwarg' ,'' )
@@ -112,23 +112,23 @@ class BasicViewSet(viewsets.ViewSet):
112112def list (self ,request ,* args ,** kwargs ):
113113return Response ({'method' :'list' })
114114
115- @detail_route (methods = ['post' ])
115+ @action (methods = ['post' ], detail = True )
116116def action1 (self ,request ,* args ,** kwargs ):
117117return Response ({'method' :'action1' })
118118
119- @detail_route (methods = ['post' ])
119+ @action (methods = ['post' ], detail = True )
120120def action2 (self ,request ,* args ,** kwargs ):
121121return Response ({'method' :'action2' })
122122
123- @detail_route (methods = ['post' ,'delete' ])
123+ @action (methods = ['post' ,'delete' ], detail = True )
124124def action3 (self ,request ,* args ,** kwargs ):
125125return Response ({'method' :'action2' })
126126
127- @detail_route ( )
127+ @action ( detail = True )
128128def link1 (self ,request ,* args ,** kwargs ):
129129return Response ({'method' :'link1' })
130130
131- @detail_route ( )
131+ @action ( detail = True )
132132def link2 (self ,request ,* args ,** kwargs ):
133133return Response ({'method' :'link2' })
134134
@@ -297,7 +297,7 @@ def setUp(self):
297297class TestViewSet (viewsets .ModelViewSet ):
298298permission_classes = []
299299
300- @detail_route (methods = ['post' ],permission_classes = [permissions .AllowAny ])
300+ @action (methods = ['post' ], detail = True ,permission_classes = [permissions .AllowAny ])
301301def custom (self ,request ,* args ,** kwargs ):
302302return Response ({
303303'permission_classes' :self .permission_classes
@@ -315,14 +315,14 @@ def test_action_kwargs(self):
315315
316316class TestActionAppliedToExistingRoute (TestCase ):
317317"""
318- Ensure `@detail_route ` decorator raises an except when applied
318+ Ensure `@action ` decorator raises an except when applied
319319 to an existing route
320320 """
321321
322322def test_exception_raised_when_action_applied_to_existing_route (self ):
323323class TestViewSet (viewsets .ModelViewSet ):
324324
325- @detail_route (methods = ['post' ])
325+ @action (methods = ['post' ], detail = True )
326326def retrieve (self ,request ,* args ,** kwargs ):
327327return Response ({
328328'hello' :'world'
@@ -339,27 +339,27 @@ class DynamicListAndDetailViewSet(viewsets.ViewSet):
339339def list (self ,request ,* args ,** kwargs ):
340340return Response ({'method' :'list' })
341341
342- @list_route (methods = ['post' ])
342+ @action (methods = ['post' ], detail = False )
343343def list_route_post (self ,request ,* args ,** kwargs ):
344344return Response ({'method' :'action1' })
345345
346- @detail_route (methods = ['post' ])
346+ @action (methods = ['post' ], detail = True )
347347def detail_route_post (self ,request ,* args ,** kwargs ):
348348return Response ({'method' :'action2' })
349349
350- @list_route ( )
350+ @action ( detail = False )
351351def list_route_get (self ,request ,* args ,** kwargs ):
352352return Response ({'method' :'link1' })
353353
354- @detail_route ( )
354+ @action ( detail = True )
355355def detail_route_get (self ,request ,* args ,** kwargs ):
356356return Response ({'method' :'link2' })
357357
358- @list_route ( url_path = "list_custom-route" )
358+ @action ( detail = False , url_path = "list_custom-route" )
359359def list_custom_route_get (self ,request ,* args ,** kwargs ):
360360return Response ({'method' :'link1' })
361361
362- @detail_route ( url_path = "detail_custom-route" )
362+ @action ( detail = True , url_path = "detail_custom-route" )
363363def detail_custom_route_get (self ,request ,* args ,** kwargs ):
364364return Response ({'method' :'link2' })
365365