基于GenericAPIView以及五个视图扩展类写接口

基于GenericAPIView以及五个视图扩展类写接口

​ 引言,本篇文章的研究主题是视图类的由简单写法到高级写法过程换句话说不断继承不断封装最终变成扩展性大的视图类,这样写接口的选择就多了,可以个性化写出自己想写的后端代码,虽然写的代码看起来少但是很多活儿已经被被封装的类干完了,我们只需要导入一下然后继承一下最后自己属性或方法来实现,当然只是不能是一蹴而就的,过程才是最享受的,因为当我们知道这个知识点怎么来的时候,我们对该知识印象更深。而且有必要的时候可以玩儿出很多花样。所以过程远比结果重要。好啦,话不多说直接上干货!!! GenericAPIView继承了继承了APIView,从而有很多新的属性和方法,写接口之后的效果是一样的但是代码的可用性变高了,变得更加通用的。使用该类之前先了解一下它的属性和方法吧

一、基于GenericAPIView写接口

GenericAPIView属性

  1. 序列化反序列 queryset
  2. 使用序列化类 serializer_class
  3. 查询单条路由 lookup_field
  4. 过滤类的配置 filter_backends
  5. 分页类的配置 pagination_class

GenericAPIView方法

  1. 获取序列对象 get_queryset
  2. 获取单个对象 get_object
  3. 获取序列化类 get_serializer
  4. 跟过滤有关的 filter_queryset
# 表模型代码 from django.db import models   class Book(models.Model):     name = models.CharField(verbose_name='书名', max_length=32)     price = models.CharField(verbose_name='价格', max_length=32)      def __str__(self):         return self.name      # 外键 书跟出版社是一对多     publish = models.ForeignKey(to='Publish', on_delete=models.CASCADE)     # 外键 书跟作者是多对多     authors = models.ManyToManyField(to='Author')      def publish_detail(self):         return {'name': self.publish.name, 'address': self.publish.address}      def author_list(self):         list = []         for author in self.authors.all():             list.append({'name': author.name, 'phone': author.phone})         return list   class Publish(models.Model):     name = models.CharField(verbose_name='出版社名称', max_length=32)     address = models.CharField(verbose_name='出版社地址', max_length=32)      def __str__(self):         return self.name   class Author(models.Model):     name = models.CharField(verbose_name='作者姓名', max_length=32)     phone = models.CharField(verbose_name='电话号码', max_length=11)      def __str__(self):         return self.name  
# 序列化类代码 class BookSerializer(serializers.ModelSerializer):     class Meta:         # 跟book表有强关联         model = Book         # fields = ['写需要序列化的字段名',[]···]         # 如果fields = '__all__'这样写就表明序列化所有字段         fields = '__all__'         # extra_kwargs = {'字段名': {'约束条件': 约束参数},是反序列化字段         extra_kwargs = {'name': {'max_length': 8},                         'publish_detail': {'read_only': True},                         'authors_list': {'read_only': True},                         'publish': {'write_only': True},                         'authors': {'write_only': True}                         }      def validate_name(self, name):         if name.startswith('sb'):             raise ValidationError('书名不能以sb开头')         else:             return name   
'''基于GenericAPIView''' from rest_framework.generics import GenericAPIView from .models import Book from .serializer import BookSerializer from rest_framework.response import Response   class BookView(GenericAPIView):     queryset = Book.objects.all()     serializer_class = BookSerializer      def get(self, request):         objs = self.get_queryset()         ser = self.serializer_class(instance=objs, many=True)         return Response(ser.data)      def post(self, request):         ser = self.get_serializer(data=request.data)         if ser.is_valid():             ser.save()             return Response({'code': 100, 'msg': '新增成功', 'result': ser.data})         else:             return Response({'code': 101, 'msg': ser.errors})   class BookDetailView(GenericAPIView):     queryset = Book.objects.all()     serializer_class = BookSerializer      def get(self, request, pk):         obj = self.get_object()         ser = self.get_serializer(instance=obj)         return Response(ser.data)      def put(self, request, pk):         obj = self.get_object()         ser = self.get_serializer(instance=obj, data=request.data)         if ser.is_valid():             ser.save()             return Response({'code':102, 'msg':'修改成功'})         else:             return Response({'code':103, 'msg':ser.errors})      def delete(self, request, pk):         self.get_object().delete()         return Response({'code':104, 'msg':'删除成功'})  

二 、基于GenericAPIView以及五个视图扩展类写接口

首先捋一下五个视图扩展类吧!切记这五个扩展类不能单独使用,必须与GenericAPIView配合使用,需要写哪种接口就用哪个扩展类即可。

  1. 新增数据,CreateModelMixin
  2. 修改数据,UpdateModelMixin
  3. 删除数据,DestroyModelMixin
  4. 获取单个,RetrieveModelMixin
  5. 获取所有,ListModelMixin
'''基于GenericAPIView及五个视图扩展类'''  from rest_framework.generics import GenericAPIView from rest_framework.mixins import ListModelMixin, CreateModelMixin, UpdateModelMixin, RetrieveModelMixin, DestroyModelMixin from .models import Book, Author, Publish from .serializer import BookSerializer    class BookView(GenericAPIView,ListModelMixin,CreateModelMixin):     queryset = Book.objects.all()     serializer_class = BookSerializer      def get(self, request):         return self.list(request)      def post(self, request):         return self.create(request)   class BookDetailView(GenericAPIView, RetrieveModelMixin, UpdateModelMixin, DestroyModelMixin):     queryset = Book.objects.all()     serializer_class = BookSerializer      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) 

基于GenericAPIView以及五个视图扩展类写接口

发表评论

相关文章