django restフレームワーククラスベースのビュー
31565 ワード
restfullスタイルでのクラスベースのビュー
チュートリアル索引
Django REST frameworkのシリーズチュートリアルは、チュートリアルのインデックスディレクトリをクリックすることができます.
第一の方法
第2の方法
3つ目の方法
第四の方法
5つ目の方法ModelViewSet
チュートリアル索引
Django REST frameworkのシリーズチュートリアルは、チュートリアルのインデックスディレクトリをクリックすることができます.
第一の方法
from rest_framework.views import APIView
class all_classroom(APIView):
def get(self,request):
classroom = Classroom.objects.all()
ser = serailizers_classroom(classroom,many=True)
return Response(ser.data,status=200)
class single_classroom(APIView):
def get(self,request,pk):
classroom = Classroom.objects.get(pk=pk)
ser = serailizers_classroom(classroom)
return Response(ser.data,status=200)
def put(self,request,pk):
classroom = Classroom.objects.get(pk=pk)
ser = serailizers_classroom(classroom,data=request.data)
if ser.is_valid():
ser.save()
return Response(ser.data,status=200)
return Response(ser.errors)
def delete(self,request,pk):
classroom = Classroom.objects.get(pk=pk)
classroom.delete()
return Response(status=200)
## url
from django.conf.urls import url
from django.contrib import admin
from django.urls import path, include
from day1106_app import urls
urlpatterns = [
path('admin/', admin.site.urls),
url('^', include(urls)),
]
## url
from django.conf.urls import url
from day1106_app.views import all_classroom, single_classroom
urlpatterns = [
url('^classroom/$', all_classroom.as_view()),
url('^classroom/(?P\d+)/$' , single_classroom.as_view())
]
第2の方法
from rest_framework.generics import GenericAPIView
from rest_framework import mixins
class all_classroom(GenericAPIView,mixins.ListModelMixin,mixins.CreateModelMixin):
queryset = Classroom.objects.all()
serializer_class = serailizers_classroom
def get(self,request):
return self.list(request)
def post(self,request):
return self.create(request)
class single_classroom(GenericAPIView,mixins.RetrieveModelMixin,mixins.DestroyModelMixin,mixins.UpdateModelMixin):
queryset = Classroom.objects.all()
serializer_class = serailizers_classroom
def get(self,request,pk):
return self.retrieve(request)
def put(self,request,pk):
return self.update(request)
def delete(self,request,pk):
return self.destroy(request)
## url
from django.conf.urls import url
from django.contrib import admin
from django.urls import path, include
from day1106_app import urls
urlpatterns = [
path('admin/', admin.site.urls),
url('^', include(urls)),
]
## url
from django.conf.urls import url
from day1106_app.views import all_classroom, single_classroom
urlpatterns = [
url('^classroom/$', all_classroom.as_view()),
url('^classroom/(?P\d+)/$' , single_classroom.as_view())
]
3つ目の方法
from rest_framework.viewsets import GenericViewSet
from rest_framework.mixins import ListModelMixin,CreateModelMixin,UpdateModelMixin,DestroyModelMixin,RetrieveModelMixin
class all_classroom(GenericViewSet,ListModelMixin,CreateModelMixin):
queryset = Classroom.objects.all()
serializer_class = serailizers_classroom
class single_classroom(GenericViewSet,UpdateModelMixin,DestroyModelMixin,RetrieveModelMixin):
queryset = Classroom.objects.all()
serializer_class = serailizers_classroom
## url
from django.conf.urls import url
from django.contrib import admin
from django.urls import path, include
from day1106_app import urls
urlpatterns = [
path('admin/', admin.site.urls),
url('^', include(urls)),
]
## url
from django.conf.urls import url
from day1106_app.views import all_classroom, single_classroom
urlpatterns = [
url('^classroom/$', all_classroom.as_view({'get':'list','post':'create'})),
url('^classroom/(?P\d+)$' , single_classroom.as_view({'get':'retrieve','put':'update','delete':'destroy'}))
]
第四の方法
from rest_framework.viewsets import GenericViewSet
from rest_framework.generics import CreateAPIView,ListAPIView,RetrieveAPIView,UpdateAPIView,DestroyAPIView
class all_classroom(GenericViewSet,CreateAPIView,ListAPIView,RetrieveAPIView,UpdateAPIView,DestroyAPIView):
queryset = Classroom.objects.all()
serializer_class = serailizers_classroom
## url
from django.conf.urls import url
from django.contrib import admin
from django.urls import path, include
from day1106_app import urls
urlpatterns = [
path('admin/', admin.site.urls),
url('^', include(urls)),
]
## url
from django.views.generic import TemplateView
from rest_framework.routers import DefaultRouter
from day1106_app.views import all_classroom, all_students
rout = DefaultRouter()
rout.register('classroom',all_classroom)
rout.register('students',all_students)
urlpatterns = [
url('^$', TemplateView.as_view(template_name='index.html')),
]
urlpatterns += rout.urls
5つ目の方法ModelViewSet
from rest_framework.viewsets import ModelViewSet
class all_classroom(ModelViewSet):
queryset = Classroom.objects.all()
serializer_class = serailizers_classroom
class all_students(ModelViewSet):
queryset = Students.objects.all()
serializer_class = serailizers_students
## url
from django.conf.urls import url
from django.contrib import admin
from django.urls import path, include
from day1106_app import urls
urlpatterns = [
path('admin/', admin.site.urls),
url('^', include(urls)),
]
## url
from django.views.generic import TemplateView
from rest_framework.routers import DefaultRouter
from day1106_app.views import all_classroom, all_students
rout = DefaultRouter()
rout.register('classroom',all_classroom)
rout.register('students',all_students)
urlpatterns = [
url('^$', TemplateView.as_view(template_name='index.html')),
]
urlpatterns += rout.urls