APIView
案例-使用APIView改写RestAPI
# views.py
from rest_framework.views import APIView
from rest_framework import status
from django.http import Http404
from booktest.serializers import BookInfoSerializer
# /books/
class BookListView(APIView):
"""
获取所有图书、增加图书
"""
def get(self, request):
"""
获取所有的图书数据
"""
queryset = BookInfo.objects.all()
# 序列化所有图书数据
serializer = BookInfoSerializer(queryset, many=True)
return Response(serializer.data)
def post(self, request):
"""
新增一个图书数据
"""
# 反序列化-数据校验
serializer = BookInfoSerializer(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(APIView):
"""
获取、修改、删除指定图书
"""
def get(self, request, pk):
"""
获取指定图书
"""
try:
book = BookInfo.objects.get(pk=pk)
except BookInfo.DoesNotExist:
raise Http404
# 将图书数据进行序列化
serializer = BookInfoSerializer(book)
return Response(serializer.data)
def put(self, request, pk):
"""
修改指定图书
"""
try:
book = BookInfo.objects.get(pk=pk)
except BookInfo.DoesNotExist:
raise Http404
# 反序列化-数据校验
serializer = BookInfoSerializer(book, data=request.data)
serializer.is_valid(raise_exception=True)
# 反序列化-数据保存(save内部会调用序列化器类的update方法)
serializer.save()
return Response(serializer.data)
def delete(self, request, pk):
"""
删除指定图书:
"""
try:
book = BookInfo.objects.get(pk=pk)
except BookInfo.DoesNotExist:
raise Http404
book.delete()
return Response(status=status.HTTP_204_NO_CONTENT)