标签:cts ram let mod ret frame ted *args EAP
class BookViewSet(mixins.ListModelMixin, mixins.CreateModelMixin, generics.GenericAPIView): queryset = Book.objects.all() serializer_class = BookSerializers def get(self, request, *args, **kwargs): return self.list(request, *args, **kwargs) def post(self, request, *args, **kwargs): return self.create(request, *args, **kwargs) class BookDetailViewSet(mixins.RetrieveModelMixin, mixins.UpdateModelMixin, mixins.DestroyModelMixin, generics.GenericAPIView): queryset = Book.objects.all() serializer_class = BookSerializers def get(self, request, *args, **kwargs): return self.retrieve(request, *args, **kwargs) def put(self, request, *args, **kwargs): return self.update(request, *args, **kwargs) def delete(self, request, *args, **kwargs): return self.destroy(request, *args, **kwargs)
from rest_framework import mixins from rest_framework import generics class BookViewSet(generics.ListCreateAPIView): queryset = Book.objects.all() serializer_class = BookSerializers class BookDetailViewSet(generics.RetrieveUpdateDestroyAPIView): queryset = Book.objects.all() serializer_class = BookSerializers
url.py:
path(‘books/‘, views.BookViewSet.as_view({‘get‘: ‘list‘, ‘post‘: ‘create‘})), re_path(r‘^books/(?P<pk>\d+)$‘, views.BookViewSet.as_view({ ‘get‘:‘retrieve‘, ‘put‘: ‘update‘, ‘delete‘: ‘destroy‘}))
views.py
class BookViewSet(viewsets.ModelViewSet): queryset = Book.objects.all() serializer_class = BookSerializers
标签:cts ram let mod ret frame ted *args EAP
原文地址:https://www.cnblogs.com/lullaby/p/9573597.html