https://www.bilibili.com/video/BV1z5411D7BQ?p=19&vd_source=caabcbd2a759a67e2a3de8acbaaf08ea
view.py
from sers.models import Book
from rest_framework import serializers
from rest_framework.mixins import ListModelMixin,CreateModelMixin,UpdateModelMixin,DestroyModelMixin,RetrieveModelMixin
from rest_framework.generics import RetrieveDestroyAPIView
from rest_framework.viewsets import GenericViewSet
class BookSerializers(serializers.ModelSerializer):
class Meta:
model=Book
fields = "__all__"
# ListModelMixin中的list方法获取所有数据 ----所以在路由处制定该方法即可
# CreateModelMixin中的create方法新增一条数据 ----所以在路由处制定该方法即可
class BookView(GenericViewSet,ListModelMixin,CreateModelMixin):
# 获取所有书籍---操作数据库
queryset = Book.objects.all()
# 序列化模型(序列化器)
serializer_class = BookSerializers
url.py
urlpatterns = [
path('admin/', admin.site.urls),
# 新
url('^book/$', views.BookView.as_view({"get":"list","post":"create"})),
# 新
# 加有名分组?P<pk>
url('^book/(?P<pk>\d+)/$', views.BooketailView.as_view()),
]
标签:进阶,--,rest,framework,Book,url,import,ViewSet,class From: https://www.cnblogs.com/kaibindirver/p/16828992.html