10
10
11
11
from rest_framework import permissions , serializers , viewsets
12
12
from rest_framework .compat import get_regex_pattern
13
- from rest_framework .decorators import detail_route , list_route
13
+ from rest_framework .decorators import action
14
14
from rest_framework .response import Response
15
15
from rest_framework .routers import DefaultRouter , SimpleRouter
16
16
from rest_framework .test import APIRequestFactory
@@ -66,12 +66,12 @@ def get_object(self, *args, **kwargs):
66
66
67
67
68
68
class RegexUrlPathViewSet (viewsets .ViewSet ):
69
- @list_route ( url_path = 'list/(?P<kwarg>[0-9]{4})' )
69
+ @action ( detail = False , url_path = 'list/(?P<kwarg>[0-9]{4})' )
70
70
def regex_url_path_list (self , request , * args , ** kwargs ):
71
71
kwarg = self .kwargs .get ('kwarg' , '' )
72
72
return Response ({'kwarg' : kwarg })
73
73
74
- @detail_route ( url_path = 'detail/(?P<kwarg>[0-9]{4})' )
74
+ @action ( detail = True , url_path = 'detail/(?P<kwarg>[0-9]{4})' )
75
75
def regex_url_path_detail (self , request , * args , ** kwargs ):
76
76
pk = self .kwargs .get ('pk' , '' )
77
77
kwarg = self .kwargs .get ('kwarg' , '' )
@@ -111,23 +111,23 @@ class BasicViewSet(viewsets.ViewSet):
111
111
def list (self , request , * args , ** kwargs ):
112
112
return Response ({'method' : 'list' })
113
113
114
- @detail_route (methods = ['post' ])
114
+ @action (methods = ['post' ])
115
115
def action1 (self , request , * args , ** kwargs ):
116
116
return Response ({'method' : 'action1' })
117
117
118
- @detail_route (methods = ['post' ])
118
+ @action (methods = ['post' ])
119
119
def action2 (self , request , * args , ** kwargs ):
120
120
return Response ({'method' : 'action2' })
121
121
122
- @detail_route (methods = ['post' , 'delete' ])
122
+ @action (methods = ['post' , 'delete' ])
123
123
def action3 (self , request , * args , ** kwargs ):
124
124
return Response ({'method' : 'action2' })
125
125
126
- @detail_route ()
126
+ @action ()
127
127
def link1 (self , request , * args , ** kwargs ):
128
128
return Response ({'method' : 'link1' })
129
129
130
- @detail_route ()
130
+ @action ()
131
131
def link2 (self , request , * args , ** kwargs ):
132
132
return Response ({'method' : 'link2' })
133
133
@@ -296,7 +296,7 @@ def setUp(self):
296
296
class TestViewSet (viewsets .ModelViewSet ):
297
297
permission_classes = []
298
298
299
- @detail_route (methods = ['post' ], permission_classes = [permissions .AllowAny ])
299
+ @action (methods = ['post' ], permission_classes = [permissions .AllowAny ])
300
300
def custom (self , request , * args , ** kwargs ):
301
301
return Response ({
302
302
'permission_classes' : self .permission_classes
@@ -321,7 +321,7 @@ class TestActionAppliedToExistingRoute(TestCase):
321
321
def test_exception_raised_when_action_applied_to_existing_route (self ):
322
322
class TestViewSet (viewsets .ModelViewSet ):
323
323
324
- @detail_route (methods = ['post' ])
324
+ @action (methods = ['post' ])
325
325
def retrieve (self , request , * args , ** kwargs ):
326
326
return Response ({
327
327
'hello' : 'world'
@@ -338,27 +338,27 @@ class DynamicListAndDetailViewSet(viewsets.ViewSet):
338
338
def list (self , request , * args , ** kwargs ):
339
339
return Response ({'method' : 'list' })
340
340
341
- @list_route (methods = ['post' ])
341
+ @action (methods = ['post' ], detail = False )
342
342
def list_route_post (self , request , * args , ** kwargs ):
343
343
return Response ({'method' : 'action1' })
344
344
345
- @detail_route (methods = ['post' ])
345
+ @action (methods = ['post' ])
346
346
def detail_route_post (self , request , * args , ** kwargs ):
347
347
return Response ({'method' : 'action2' })
348
348
349
- @list_route ( )
349
+ @action ( detail = False )
350
350
def list_route_get (self , request , * args , ** kwargs ):
351
351
return Response ({'method' : 'link1' })
352
352
353
- @detail_route ()
353
+ @action ()
354
354
def detail_route_get (self , request , * args , ** kwargs ):
355
355
return Response ({'method' : 'link2' })
356
356
357
- @list_route ( url_path = "list_custom-route" )
357
+ @action ( detail = False , url_path = "list_custom-route" )
358
358
def list_custom_route_get (self , request , * args , ** kwargs ):
359
359
return Response ({'method' : 'link1' })
360
360
361
- @detail_route (url_path = "detail_custom-route" )
361
+ @action (url_path = "detail_custom-route" )
362
362
def detail_custom_route_get (self , request , * args , ** kwargs ):
363
363
return Response ({'method' : 'link2' })
364
364
0 commit comments