rest_famework 增删改查初第三阶段(高级,此阶段是优化第二阶段的代码)的使用

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 结果:
rest_famework 增删改查初第三阶段(高级,此阶段是优化第二阶段的代码)的使用
 

上一篇:JS组件系列——BootstrapTable+KnockoutJS实现增删改查解决方案(四):自定义T4模板快速生成页面


下一篇:额的神啊~~可爱的Visual Studio把项目模板扔了