Minin混合类
链接:
https://www.bilibili.com/video/BV1z5411D7BQ?p=16&vd_source=caabcbd2a759a67e2a3de8acbaaf08ea
https://www.bilibili.com/video/BV1z5411D7BQ?p=17&vd_source=caabcbd2a759a67e2a3de8acbaaf08ea
views.py
from rest_framework.generics import GenericAPIView
from rest_framework.response import Response
from sers.models import Book
from rest_framework import serializers
from rest_framework.mixins import ListModelMixin,CreateModelMixin,UpdateModelMixin,DestroyModelMixin,RetrieveModelMixin
class BookSerializers(serializers.ModelSerializer):
class Meta:
model=Book
fields = "__all__"
# ListModelMixin中的list方法获取所有数据
# CreateModelMixin中的create方法新增一条数据
class BookView(GenericAPIView,ListModelMixin,CreateModelMixin):
# 获取所有书籍---操作数据库
queryset = Book.objects.all()
# 序列化模型(序列化器)
serializer_class = BookSerializers
# 获取所有数据
def get(self, requet):
return self.list(requet)
# 新增数据
def post(self, request):
return self.create(request)
# RetrieveModelMixin中retrieve方法,为根据id查看单条
# UpdateModelMixin中update方法,为根据id更新某条
# DestroyModelMixin中destroy方法,为根据id删除某条
class BooketailView(GenericAPIView,UpdateModelMixin,RetrieveModelMixin,DestroyModelMixin):
# 获取所有书籍---操作数据库
queryset = Book.objects.all()
#序列化模型(序列化器)
serializer_class = BookSerializers
# 带参数id查询
def get(self, request, pk):
return self.retrieve(request, pk)
def put(self, request, pk):
return self.update(request, pk)
def delete(self,request,pk):
return self.destroy(request,pk)
models.py
from django.db import models
# Create your models here.
class Book(models.Model):
title=models.CharField(max_length=55,verbose_name="书籍名称")
price=models.IntegerField(verbose_name="价格")
# pub_date=models.DateField(verbose_name="出版日期")
pub_date = models.CharField(max_length=55,verbose_name="出版日期")
url.py
urlpatterns = [
path('admin/', admin.site.urls),
# 新
url('^book/$', views.BookView.as_view()),
# 新
# 加有名分组?P<pk>
url('^book/(?P<pk>\d+)/$', views.BooketailView.as_view()),
]
Minin混合类高度封装
from sers.models import Book
from rest_framework import serializers
from rest_framework.mixins import ListModelMixin,CreateModelMixin,UpdateModelMixin,DestroyModelMixin,RetrieveModelMixin
# ListCreateAPIView方法集成了GenericAPIView,ListModelMixin,CreateModelMixin 三个类,且写了list 和 create的方法
from rest_framework.generics import ListCreateAPIView
# RetrieveDestroyAPIView方法集成了GenericAPIView,UpdateModelMixin,RetrieveModelMixin,DestroyModelMixin 四个类
# 且写了retrieve、update、destroy 的方法
from rest_framework.generics import RetrieveDestroyAPIView
# rest_framework.generics 库里面还有其他灵活的方法 如下:
# RetrieveAPIView 只有查的方法--get
# RetrieveDestroyAPIView 查看加删除的方法 get和delete
# RetrieveUpdateDestroyAPIView 查看加更新的方法 get和updata
class BookSerializers(serializers.ModelSerializer):
class Meta:
model=Book
fields = "__all__"
# ListModelMixin中的list方法获取所有数据
# CreateModelMixin中的create方法新增一条数据
class BookView(ListCreateAPIView):
# 获取所有书籍---操作数据库
queryset = Book.objects.all()
# 序列化模型(序列化器)
serializer_class = BookSerializers
# RetrieveModelMixin中retrieve方法,为根据id查看单条
# UpdateModelMixin中update方法,为根据id更新某条
# DestroyModelMixin中destroy方法,为根据id删除某条
class BooketailView(RetrieveDestroyAPIView):
# 获取所有书籍---操作数据库
queryset = Book.objects.all()
#序列化模型(序列化器)
serializer_class = BookSerializers