views.py
from django.shortcuts import render # Create your views here. from rest_framework.generics import GenericAPIView from rest_framework.response import Response from rest_framework.views import APIView from bookset.models import BookInfo from .serializers import BookInfoModelSerializer ######################################################## '''APIiew的使用,和django里面类视图的使用一致''' ####################################################### class BookinfoAPIView(APIView): def get(self,request): #1操作数据库 books=BookInfo.objects.all() #2序列化 serializer=BookInfoModelSerializer(instance=books,many=True) #3响应数据 return Response(serializer.data) def post(self,request): #1.获取客户端传来的数据 data=request.data #2. 反序列化[验证和保存数据] serializer=BookInfoModelSerializer(data=data) serializer.is_valid(raise_exception=True) instance=serializer.save() #3.响应数据 #把新创建的模型对象序列化提供给客户端 serializer=BookInfoModelSerializer(instance=instance) return Response(serializer.data) # class BookInfo2APIView(APIView): # '''获取一条数据''' # def get(self,request,pk): # #1.操作数据库 # book=BookInfo.objects.get(pk=pk) # print(book) # # #2x序列化数据 # serializer=BookInfoModelSerializer(instance=book) # #3响应数据 # return Response(serializer.data) class BookInfo2APIView(APIView): def get(self,request,pk): """获取一条数据""" # 1. 操作数据库 book = BookInfo.objects.get(pk=pk) # 2. 序列化 serializer = BookInfoModelSerializer(instance=book) #3. 响应数据 return Response(serializer.data) ################################################### #GenericAPIView的操作,继承了APIView基础,针对序列化器 #对象和获取模型数据进行了简化代码以及功能扩展 ################################################### from rest_framework.generics import GenericAPIView from ser.serializers import BookInfoModelSerializer class BookInfoGenericAPIIView(GenericAPIView): #必须声明当前视图类中操作的模型数据是什么 queryset = BookInfo.objects.all() #可以声明当前视图类中要调用序列化器是什么 serializer_class = BookInfoModelSerializer def get(self,request): '''获取所有数据''' #1.数据库操作 # self.get_queryset() 是GenericAPIView提供的方法,用于获取多条数据 data_list=self.get_queryset() #2.序列化 serializer=self.get_serializer(instance=data_list,many=True) #响应数据 return Response(serializer.data) def post(self,request): '''添加数据''' #1. 接受客户端提交数据 data=request.data #2. 反序列化数据 serializer=self.get_serializer(data=data,) serializer.is_valid(raise_exception=True) instance=serializer.save() #3.序列化 serializer=self.get_serializer(instance=instance) #4,响应数据 return Response(serializer.data) class BookInfo2GenericAPIIView(GenericAPIView): queryset = BookInfo.objects.all() serializer_class = BookInfoModelSerializer def get(self,request,pk): '''获取一条数据''' #1. 根据id获取一条数据,使用self.get_object必须在路由中声明 #pk会自动传 book=self.get_object() #2.序列化 serializer=self.get_serializer(instance=book) #3.返回数据 return Response(serializer.data) ################################################################################# """GenericAPIView视图类可以结合视图扩展类,快速实现大量简化视图方法的代码""" ################################################################################# from rest_framework import mixins from rest_framework.generics import GenericAPIView from bookset.models import BookInfo from ser.serializers import BookInfoModelSerializer class BookInfoMixinAPIView(GenericAPIView,mixins.ListModelMixin,mixins.CreateModelMixin): queryset = BookInfo.objects.all() serializer_class = BookInfoModelSerializer def get(self,request): '''获取所有数据''' return self.list(request) def post(self,request): '''添加数据''' return self.create(request) class BookInfo2MixinAPIView(GenericAPIView,mixins.RetrieveModelMixin,mixins.UpdateModelMixin,mixins.DestroyModelMixin): queryset = BookInfo.objects.all() serializer_class = BookInfoModelSerializer 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) #################################################### '''drf开发接口中,使用GenericAPIView和食欲扩展类结合起来是完成接口是意见很常见的事情, 所以,drf的作者提前帮我们把GenericAPIView和视图扩展类结合的子类也声明了 视图子类中还帮我们定义对应接口的视图方我们无需在视图中重复编写对应的视图方法了''' #################################################### # ListAPIView 是 GenericAPIView 和 ListMOdelMixin组合子类 from rest_framework.generics import ListAPIView,CreateAPIView class BookInfoSonAPIView(ListAPIView,CreateAPIView): queryset = BookInfo.objects.all() serializer_class = BookInfoModelSerializer from rest_framework.generics import RetrieveAPIView,DestroyAPIView,UpdateAPIView from rest_framework.generics import RetrieveUpdateDestroyAPIView class BookInfo2SonAPIView(RetrieveAPIView,DestroyAPIView,UpdateAPIView): queryset = BookInfo.objects.all() serializer_class = BookInfoModelSerializer ################################################### '''上面可以看到类属性重复,如果两个视图类集中成一个视图类,那么就要解决原来一个视图类中只能出现一个http请求方法的情况 drf中提供了视图集帮我们通过改变路由和视图方法的绑定来解决''' ################################################### from rest_framework.viewsets import ViewSet from bookset.models import BookInfo from .serializers import BookInfoModelSerializer from rest_framework.response import Response class BookInfoAPIViewSet(ViewSet): def get_list(self,request): '''获取所有数据''' #1. 操作数据库 books=BookInfo.objects.all() #2. 序列化 serializer=BookInfoModelSerializer(instance=books,many=True) #3. 返回数据 return Response(serializer.data) def get_one(self,request,pk): #获取对应数据 book=BookInfo.objects.get(pk=pk) #序列化 serializer=BookInfoModelSerializer(instance=book) #相应数据 return Response(serializer.data) def get_top_5(self,request): '''获取评论最多的5条数据''' #操作数据库 books=BookInfo.objects.order_by('-bread')[:5] #序列化 serializer=BookInfoModelSerializer(instance=books,many=True) #响应数据 return Response(serializer.data) ################################################# '''上面代码又回到了最初的APIView时代,所以我们可以使用GeneriacViewSet来简化''' ################################################# from rest_framework.viewsets import GenericViewSet from bookset.models import BookInfo from .serializers import BookInfoModelSerializer from rest_framework.response import Response from rest_framework.mixins import ListModelMixin,RetrieveModelMixin,UpdateModelMixin class BookInfoGenericSet(GenericViewSet,ListModelMixin,RetrieveModelMixin): queryset = BookInfo.objects.all() serializer_class = BookInfoModelSerializer def get_list(self,request): '''获取所有数据''' return self.list(request) def get_one(self,request,pk): '''获取一条数据''' return self.retrieve(request,pk) ################################################# '''上面代码虽然简化许多代码,但是drf中针对常用的5个接口,事实上有提供了预设类给我们直接使用''' ################################################# from rest_framework.viewsets import ModelViewSet,ReadOnlyModelViewSet from bookset.models import BookInfo from .serializers import BookInfoModelSerializer from rest_framework.response import Response class BookInfoModelViewSet(ModelViewSet): queryset = BookInfo.objects.all() serializer_class = BookInfoModelSerializer def get_top_5(self,requst): '''获取评论最多的五条评论''' #操作数据库 books=BookInfo.objects.order_by('-bread')[:5] #序列化 serializer=BookInfoModelSerializer(instance=books,many=True) #返回数据 return Response(serializer.data)
urls.py
#!/usr/bin/env python # -*- coding: utf-8 -*- # author tom from django.urls import path, re_path from . import views urlpatterns = [ path('books/', views.BookinfoAPIView.as_view()), re_path('books/(?P<pk>d+)/', views.BookInfo2APIView.as_view()), # GenericAPIView path('books2/', views.BookInfoGenericAPIIView.as_view()), re_path('books2/(?P<pk>d+)/', views.BookInfo2GenericAPIIView.as_view()), # ListModelMixin,RetrieveModelMixin path('books3/', views.BookInfoMixinAPIView.as_view()), re_path('books3/(?P<pk>d+)/', views.BookInfo2MixinAPIView.as_view()), path('books4/', views.BookInfoSonAPIView.as_view()), re_path('books4/(?P<pk>d+)/', views.BookInfo2SonAPIView.as_view()), path('books5/', views.BookInfoAPIViewSet.as_view({"get": "get_list"})), re_path('books5/(?P<pk>d+)/', views.BookInfoAPIViewSet.as_view({"get": "get_one"})), path('books5/top5/', views.BookInfoAPIViewSet.as_view({'get': 'get_top_5'})), path('books6/', views.BookInfoGenericSet.as_view({'get': 'get_list'})), re_path('books6/(?P<pk>d+)/', views.BookInfoAPIViewSet.as_view({'get': 'get_one'})), # 针对ModelViewSet提供了5个视图方法,路由如下 path('books7/', views.BookInfoModelViewSet.as_view({'get': 'list'})), path("books7/", views.BookInfoModelViewSet.as_view({"post": "create"})), path('books7/top5/', views.BookInfoModelViewSet.as_view({'get': 'get_top_5'})), re_path("books7/(?P<pk>d+)/", views.BookInfoModelViewSet.as_view({"get": "retrieve"})), re_path("books7/(?P<pk>d+)/", views.BookInfoModelViewSet.as_view({"put": "update"})), re_path("books7/(?P<pk>d+)/", views.BookInfoModelViewSet.as_view({"delete": "destroy"})), ]
serializer.py
#!/usr/bin/env python # -*- coding: utf-8 -*- #author tom ################################################## # 序列化器之序列化 ################################################## from rest_framework import serializers from bookset.models import BookInfo class BookInfoSerializer(serializers.Serializer): #自定义要序列化反序列化的字段 id=serializers.IntegerField(label='主键id',read_only=True) btitle=serializers.CharField(label='图书标题') bpub_date=serializers.DateField(label='出版日期') bread=serializers.IntegerField(label='阅读量') bcomment=serializers.IntegerField(label='评论量') is_delete=serializers.BooleanField(label="逻辑删除") ########################################################### # 2 序列化器的反序列化阶段使用 # 主要用户验证数据和字典数据转换成模型 ####################################################### from rest_framework import serializers # is_valid调用验证方式:字段选项validators->自定义验证方法[单选项]->自定义验证方法[多选项] #自定义字段选选项函数(比较少用,一般用内置的) def check_btitle(data): if data=='西厢记': raise serializers.ValidationError('西厢记也好黄啊') #一定要返回数据 return data class BookInfo2Serializer(serializers.Serializer): #自定义反序列化的字段 btitle = serializers.CharField(label='图书标题',min_length=1,max_length=128,validators=[check_btitle]) bpub_date = serializers.DateField(label='出版日期') bread = serializers.IntegerField(label='阅读量',min_value=0) bcomment = serializers.IntegerField(label='评论量',default=0) #表示当前字段可以不填 is_delete = serializers.BooleanField(label="逻辑删除") #自定义验证方法,单字段校验[验证单个字段,可以有多个方法] #格式:def validate_字段名(self,data):#data当前字段对应值 #data是形参,sub写,代表的是当前字段对应的值 def validate_btitle(self,data): if data=='红楼梦': #抛出错误 raise serializers.ValidationError('红楼梦太色请了') #校验过后一定要把数据值返回,否则数据值为空 return data # 多字段校验数据值data是所有字典的内容,字典类型 def validate(self,data): bread=data.get('bread') bcomment=data.get('bcomment') if bread>=bcomment: return data raise serializers.ValidationError('阅读量小于评论量,太假了') def create(self,validated_data): ''' view视图的save会调用此方法 保存数据,把字典转换成模型 :param validated_data: 客户提交过来的,并且经过验证的数据 :return: ''' instance=BookInfo.objects.create( btitle=validated_data.get('btitle'), bread=validated_data.get('bread'), bcomment=validated_data.get('bcomment'), bpub_date=validated_data.get('bpub_date'), is_delete = validated_data.get('is_delete') ) return instance # def update(self,instance,validated_data): # ''' # 更新数据 # instance 本次跟新操作的模型对象 # validated_data: 客户提交过来,并经过验证的数据 # :param instance: # :param validated_data: # :return: # ''' # instance.btitle=validated_data.get("btitle"), # instance.bread = validated_data.get("bread"), # instance.bcomment = validated_data.get("bcomment"), # instance.bpub_date = validated_data.get("bpub_date"), # instance.is_delete = validated_data.get("is_delete"), # # #调用orm在操作 # instance.save() # #返回模型对象 # return instance def update(self,instance,validated_data): """更新数据 instance 本次更新操作的模型对象 validated_data 客户端提交过来,并经过验证的数据 """ instance.btitle=validated_data.get('btitle') instance.bread=validated_data.get('bread') instance.bcomment=validated_data.get('bcomment') instance.bpub_date=validated_data.get('bpub_date') instance.is_delete=validated_data.get('is_delete') # 调用ORM的保存更新操作 instance.save() # 返回模型对象 return instance ######################################################### #3. 模型序列化器 # 1.可以帮助我们自动完成字声明[主要从模型中的字段声明里面提取过来] # 2.模型序列化器要可以帮我们声明create和update的方法和代码,所我们不需要自己写crate和pub_date ######################################################### from rest_framework import serializers from bookset.models import BookInfo class BookInfoModelSerializer(serializers.ModelSerializer): #模型序列化器也可以自定义验证字段(某些数据库不存在,但是需要前端传过来的,可以进行自定义) #例如:验证码,确认密码 class Meta: model=BookInfo fields="__all__" #可以给模型序列 # 化器里面指定的字段设置限制选项 extra_kwargs={ 'bread':{"min_value":0,"required":True}, } # 自定义验证方法[验证单个字段,可以有多个方法] # def validate_<字段名>(self,data): # data当前字段对应的值 def validate_btitle(self, data): # 例如,图书名不能是红楼梦 if data == "红楼梦": # 抛出错误 raise serializers.ValidationError("红楼梦是禁书~") # 验证方法中,把数据值必须返回给字段,否则字段值为空 return data # 自定义验证方法[验证多个或者所有字段,只能出现一次] def validate(self, data): # data 这个是所有字段的内容,字典类型 bread = data.get("bread") bcomment = data.get("bcomment") if bread >= bcomment: return data raise serializers.ValidationError("阅读量小于评论量,数据太假了")
model.py
from django.db import models #定义图书模型类BookInfo class BookInfo(models.Model): btitle = models.CharField(max_length=20, verbose_name='图书标题') bpub_date = models.DateField(verbose_name='出版时间') bread = models.IntegerField(default=0, verbose_name='阅读量') bcomment = models.IntegerField(default=0, verbose_name='评论量') is_delete = models.BooleanField(default=False, verbose_name='逻辑删除') class Meta: db_table = 'book' # 指明数据库表名 verbose_name = '图书' # 在admin站点中显示的名称 verbose_name_plural = verbose_name # 显示的复数名称 def __str__(self): """定义每个数据对象的显示信息""" return "图书:《"+self.btitle+"》" #定义英雄模型类HeroInfo class HeroInfo(models.Model): GENDER_CHOICES = ( (0, 'female'), (1, 'male') ) hname = models.CharField(max_length=20, verbose_name='名称') hgender = models.SmallIntegerField(choices=GENDER_CHOICES, default=0, verbose_name='性别') hcomment = models.CharField(max_length=200, null=True, verbose_name='描述信息') hbook = models.ForeignKey(BookInfo, on_delete=models.CASCADE, verbose_name='图书') # 外键 is_delete = models.BooleanField(default=False, verbose_name='逻辑删除') class Meta: db_table = 'heros' verbose_name = '英雄' verbose_name_plural = verbose_name def __str__(self): return self.hname