2
2
from .serializers import FooSerializer , BarSerializer
3
3
4
4
from rest_framework import viewsets
5
+ from rest_framework .decorators import api_view
6
+ from rest_framework .views import APIView
7
+ from rest_framework .request import Request
8
+ from rest_framework .response import Response
5
9
6
-
10
+ # Viewsets
11
+ # see https://www.django-rest-framework.org/tutorial/quickstart/
7
12
class FooViewSet (viewsets .ModelViewSet ):
8
13
queryset = Foo .objects .all ()
9
14
serializer_class = FooSerializer
@@ -13,8 +18,25 @@ class BarViewSet(viewsets.ModelViewSet):
13
18
queryset = Bar .objects .all ()
14
19
serializer_class = BarSerializer
15
20
21
+ # class based view
22
+ # see https://www.django-rest-framework.org/api-guide/views/#class-based-views
23
+
24
+ class MyClass (APIView ):
25
+ def initial (self , request , * args , ** kwargs ):
26
+ # this method will be called before processing any request
27
+ super ().initial (request , * args , ** kwargs )
28
+
29
+ def get (self , request ):
30
+ return Response ("GET request" )
31
+
32
+ def post (self , request ):
33
+ return Response ("POST request" )
34
+
35
+
36
+ # function based view
37
+ # see https://www.django-rest-framework.org/api-guide/views/#function-based-views
38
+
16
39
17
- # this is pure django
18
- from django .http import HttpResponse , HttpRequest
19
- def example (request : HttpRequest ):
20
- return HttpResponse ("example" )
40
+ @api_view (["GET" , "POST" ])
41
+ def function_based_view (request : Request ):
42
+ return Response ({"message" : "Hello, world!" })
0 commit comments