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

 

 

相关文章:

  • 2021-12-12
  • 2021-08-28
  • 2021-08-02
  • 2021-10-31
  • 2022-02-11
  • 2021-05-31
  • 2021-08-15
  • 2021-08-31
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-07-11
  • 2021-08-02
  • 2021-11-17
  • 2021-11-22
相关资源
相似解决方案