5
5
6
6
from rest_framework import filters , pagination , permissions , serializers
7
7
from rest_framework .compat import coreapi
8
+ from rest_framework .decorators import detail_route
8
9
from rest_framework .response import Response
9
10
from rest_framework .routers import DefaultRouter
10
11
from rest_framework .schemas import SchemaGenerator
@@ -27,12 +28,21 @@ class ExampleSerializer(serializers.Serializer):
27
28
b = serializers .CharField (required = False )
28
29
29
30
31
+ class AnotherSerializer (serializers .Serializer ):
32
+ c = serializers .CharField (required = True )
33
+ d = serializers .CharField (required = False )
34
+
35
+
30
36
class ExampleViewSet (ModelViewSet ):
31
37
pagination_class = ExamplePagination
32
38
permission_classes = [permissions .IsAuthenticatedOrReadOnly ]
33
39
filter_backends = [filters .OrderingFilter ]
34
40
serializer_class = ExampleSerializer
35
41
42
+ @detail_route (methods = ['post' ], serializer_class = AnotherSerializer )
43
+ def custom_action (self , request , pk ):
44
+ return super (ExampleSerializer , self ).retrieve (self , request )
45
+
36
46
37
47
class ExampleView (APIView ):
38
48
permission_classes = [permissions .IsAuthenticatedOrReadOnly ]
@@ -120,6 +130,16 @@ def test_authenticated_request(self):
120
130
coreapi .Field ('pk' , required = True , location = 'path' )
121
131
]
122
132
),
133
+ 'custom_action' : coreapi .Link (
134
+ url = '/example/{pk}/custom_action/' ,
135
+ action = 'post' ,
136
+ encoding = 'application/json' ,
137
+ fields = [
138
+ coreapi .Field ('pk' , required = True , location = 'path' ),
139
+ coreapi .Field ('c' , required = True , location = 'form' ),
140
+ coreapi .Field ('d' , required = False , location = 'form' ),
141
+ ]
142
+ ),
123
143
'update' : coreapi .Link (
124
144
url = '/example/{pk}/' ,
125
145
action = 'put' ,
0 commit comments