url:
re_path('authors/$', views.AuthorView.as_view()),
re_path('book/(?P<pk>\d+)/$', views.BookView.as_view()),



#此阶段已经优化点了所有的retrun 返回数据 全部写到generics 父类里面去了
from rest_framework.mixins import CreateModelMixin      as create           #创建数据
from rest_framework.mixins import ListModelMixin as get_list_all #查看所有数据
from rest_framework.mixins import DestroyModelMixin as delete #删除数据
from rest_framework.mixins import RetrieveModelMixin as get_list_one #查看一条数据
from rest_framework.mixins import UpdateModelMixin as updata #更新数据


from rest_framework import mixins
from rest_framework import generics


class BookViewSet(generics.ListCreateAPIView): #class ListCreateAPIView(mixins.ListModelMixin,mixins.CreateModelMixin,GenericAPIView):

queryset = Book.objects.all()
serializer_class = BookSerializers

class BookDetailViewSet(generics.RetrieveUpdateDestroyAPIView): #class RetrieveUpdateDestroyAPIView(mixins.RetrieveModelMixin,mixins.UpdateModelMixin,mixins.DestroyModelMixin,GenericAPIView):
queryset = Book.objects.all()
serializer_class = BookSerializers

class PublishViewSet(generics.ListCreateAPIView):

queryset = Publish.objects.all()
serializer_class = PublshSerializers

class PublishDetailViewSet(generics.RetrieveUpdateDestroyAPIView):
queryset = Publish.objects.all()
serializer_class = PublshSerializers

结果:

 

 

posted on 2019-03-07 15:37  Python哥  阅读(127)  评论(0编辑  收藏  举报