GenericAPIView
案例-使用GenericAPIView改写RestAPI
# views.py
from rest_framework.generics import GenericAPIView
from rest_framework import status
from booktest.serializers import BookInfoSerializer
from booktest.models import BookInfo
# /books/
class BookListView(GenericAPIView):
"""
获取所有图书、增加图书
"""
# 指定视图所使用的序列化器类
serializer_class = BookInfoSerializer
# 指定视图所使用的查询集
queryset = BookInfo.objects.all()
def get(self, request):
"""
获取所有的图书数据
"""
queryset = self.get_queryset()
# 序列化所有图书数据
serializer = self.get_serializer(queryset, many=True)
return Response(serializer.data)
def post(self, request):
"""
新增一个图书数据
"""
# 反序列化-数据校验
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
# 反序列化-数据保存(save内部会调用序列化器类的create方法)
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
# /books/(?P<pk>\d+)/
class BookDetailView(GenericAPIView):
"""
获取、修改、删除指定图书
"""
# 指定视图所使用的序列化器类
serializer_class = BookInfoSerializer
# 指定视图所使用的查询集
queryset = BookInfo.objects.all()
def get(self, request, pk):
"""
获取指定图书
"""
instance = self.get_object()
# 将图书数据进行序列化
serializer = BookInfoSerializer(instance)
return Response(serializer.data)
def put(self, request, pk):
"""
修改指定图书
"""
instance = self.get_object()
# 反序列化-数据校验
serializer = BookInfoSerializer(instance, data=request.data)
serializer.is_valid(raise_exception=True)
# 反序列化-数据保存(save内部会调用序列化器类的update方法)
serializer.save()
return Response(serializer.data)
def delete(self, request, pk):
"""
删除指定图书:
"""
instance = self.get_object()
instance.delete()
return Response(status=status.HTTP_204_NO_CONTENT)