url:
re_path('authors/$', views.AuthorView.as_view()),
re_path('authors/(\d+)/$', views.AuthorDetailView.as_view()),


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 generics class AuthorSerializers(serializers.ModelSerializer): class Meta: model = models.Author fields = "__all__" #depth = 1 #取消注释,显示更加详细的内容,不然只显示作者的主键值 class AuthorView(create,get_list_all,delete,get_list_one,updata,generics.GenericAPIView): queryset = models.Author.objects.all() #需要提供的数据 前面名字一定要这么写 serializer_class = AuthorSerializers    #需要处理的类 前面名字一定要这么写 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 AuthorDetailView(create,get_list_all,delete,get_list_one,updata,generics.GenericAPIView): queryset = models.Author.objects.all() serializer_class = AuthorSerializers 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)

 

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