DRF 视图
视图
2个视图基类
APIView
是REST framework提供的所有视图的基类,继承自Django的View父类。
get()
:处理HTTP GET请求的方法。用于查询操作。post()
:处理HTTP POST请求的方法。用于创建操作。put()
:处理HTTP PUT请求的方法。用于更新操作。delete()
:处理HTTP DELETE请求的方法。用于删除。
from rest_framework.views import APIView
from rest_framework.response import Response
from .models import Book
class BookView(APIView):
def get(self, request):
data_list = Book.objects.all()
serializer =BookSerializer(instance=data_list, many=True)
return Response(serializer.data)
GenericAPIView
- 继承自APIView,因此它继承了APIView的所有属性和方法。
- queryset :使用的数据查询集,用于执行数据库查询。
- serializer_class:指定视图使用的序列化器,用于序列化和反序列化数据。
- get_queryset() : 返回视图使用的查询集,拿取多条数据
- get_object() : 拿取单条数据
- get_serializer() : 获取序列化类的方法。可以通过重写该方法来自定义序列化类。
from rest_framework.generics import GenericAPIView
class BookView(GenericAPIView):
queryset = Book.objects.all()
serializer_class = BookSerializer
def get(self, request, pk):
obj = self.get_object()
serializer = self.get_serializer(instance=obj)
return Response({'code': 100, 'msg': '成功', 'result': serializer.data})
5个视图扩展类
from rest_framework.mixins import CreateModelMixin, RetrieveModelMixin, DestroyModelMixin, ListModelMixin, UpdateModelMixin
继承于GenericAPIView
CreateModelMixin
- 新增数据
- create(request, *args, **kwargs)
- 处理创建对象的请求。
- self.get_serializer(data=request.data):创建一个序列化器实例
- serializer.is_valid(raise_exception=True):检查传递给序列化器的数据是否有效,如果数据无效,则会引发异常
- self.perform_create(serializer) 调用序列化器的
save()
方法创建新的对象。 - 返回包含创建的对象数据的响应。
class BookView(GenericAPIView, CreateModelMixin):
queryset = Book.objects.all()
serializer_class = BookSerializer
def get(self, request, *args, **kwargs):
return super().create(request, *args, **kwargs)
RetrieveModelMixi
-
查询一条数据
-
retrieve(request, *args, **kwargs):处理获取单个对象的请求。
class BookView(GenericAPIView, RetrieveModelMixin):
queryset = Book.objects.all()
serializer_class = BookSerializer
def post(self, request, *args, **kwargs):
return super().retrieve(request, *args, **kwargs)
DestroyModelMixin
- 删除数据
- destroy(request, *args, **kwargs):处理删除对象的请求
class BookView(GenericAPIView, DestroyModelMixin):
queryset = Book.objects.all()
serializer_class = BookSerializer
def delete(self, request, *args, **kwargs):
return super().destroy(request, *args, **kwargs)
ListModelMixin
- 查询所以数据
- list(request, *args, **kwargs):处理获取对象列表的请求。
class BookView(GenericAPIView, ListModelMixin):
queryset = Book.objects.all()
serializer_class = BookSerializer
def put(self, request, *args, **kwargs):
return super().list(request, *args, **kwargs)
UpdateModelMixin
- 更新数据
- update(request, *args, **kwargs):处理完全更新对象的请求。
class BookView(GenericAPIView, UpdateModelMixin):
queryset = Book.objects.all()
serializer_class = BookSerializer
def put(self, request, *args, **kwargs):
return super().update(request, *args, **kwargs)
9个视图子类
from rest_framework.generics import CreateAPIView, ListAPIView, RetrieveAPIView, DestroyAPIView, UpdateAPIView, ListCreateAPIView, RetrieveDestroyAPIView, RetrieveUpdateAPIView, RetrieveUpdateDestroyAPIView
class BookView(视图子类):
queryset = Book.objects.all()
serializer_class = BookSerializer
1.CreateAPIView
创建数据
- 提供 post 方法
- 继承自: GenericAPIView、CreateModelMixin
class CreateAPIView(mixins.CreateModelMixin,
GenericAPIView):
def post(self, request, *args, **kwargs):
return self.create(request, *args, **kwargs)
2.ListAPIView
查询所以数据
- 提供 get 方法
- 继承自:GenericAPIView、ListModelMixin
class ListAPIView(mixins.ListModelMixin,
GenericAPIView):
def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)
3. RetrieveAPIView
查询单条数据
- 提供 get 方法
- 继承自: GenericAPIView、RetrieveModelMixin
class RetrieveAPIView(mixins.RetrieveModelMixin,
GenericAPIView):
def get(self, request, *args, **kwargs):
return self.retrieve(request, *args, **kwargs)
4.DestroyAPIView
删除数据
- 提供 delete 方法
- 继承自:GenericAPIView、DestoryModelMixin
class DestroyAPIView(mixins.DestroyModelMixin,
GenericAPIView):
def delete(self, request, *args, **kwargs):
return self.destroy(request, *args, **kwargs)
5.UpdateAPIView
更新数据
- 提供 put 和 patch 方法
- 继承自:GenericAPIView、UpdateModelMixin
class UpdateAPIView(mixins.UpdateModelMixin,
GenericAPIView):
def put(self, request, *args, **kwargs):
return self.update(request, *args, **kwargs)
def patch(self, request, *args, **kwargs):
return self.partial_update(request, *args, **kwargs)
6.ListCreateAPIView
查询所以数据和创建数据
- 提供 get、post方法
- 继承自: GenericAPIView、ListModelMixin、CreateModelMixin
class ListCreateAPIView(mixins.ListModelMixin,
mixins.CreateModelMixin,
GenericAPIView):
def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
return self.create(request, *args, **kwargs)
7.RetrieveDestroyAPIView
查询单条数据和删除数据
- 提供 get、delete方法
- 继承自: GenericAPIView、RetrieveModelMixin、DestroyModelMixin
class RetrieveDestroyAPIView(mixins.RetrieveModelMixin,
mixins.DestroyModelMixin,
GenericAPIView):
def get(self, request, *args, **kwargs):
return self.retrieve(request, *args, **kwargs)
def delete(self, request, *args, **kwargs):
return self.destroy(request, *args, **kwargs)
8.RetrieveUpdateAPIView
查询单条数据和修改数据
- 提供 get、put、patch方法
- 继承自: GenericAPIView、RetrieveModelMixin、UpdateModelMixin
class RetrieveUpdateAPIView(mixins.RetrieveModelMixin,
mixins.UpdateModelMixin,
GenericAPIView):
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 patch(self, request, *args, **kwargs):
return self.partial_update(request, *args, **kwargs)
9.RetrieveUpdateDestroyAPIView
查询单条数据、修改数据和删除数据
- 提供 get、put、patch、delete方法
- 继承自:GenericAPIView、RetrieveModelMixin、UpdateModelMixin、DestroyModelMixin
class RetrieveUpdateDestroyAPIView(mixins.RetrieveModelMixin,mixins.UpdateModelMixin,mixins.DestroyModelMixin,GenericAPIView):
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 patch(self, request, *args, **kwargs):
return self.partial_update(request, *args, **kwargs)
def delete(self, request, *args, **kwargs):
return self.destroy(request, *args, **kwargs)
视图集
ModelViewSet
- 继承自:5个视图扩展类和GenericViewSet
class ModelViewSet(mixins.CreateModelMixin,
mixins.RetrieveModelMixin,
mixins.UpdateModelMixin,
mixins.DestroyModelMixin,
mixins.ListModelMixin,
GenericViewSet):
"""
A viewset that provides default `create()`, `retrieve()`, `update()`,
`partial_update()`, `destroy()` and `list()` actions.
"""
pass
- GenericViewSet然后继承ViewSetMixin, GenericAPIView
ViewSetMixin
- 只要继承它,路由写法就变了,必须加action
- action是请求方式和视图类中方法的映射关系
urls.py
urlpatterns = [
path('books/', BookView.as_view({'get': 'list', 'post': 'create'})),
path('books/<int:pk>/', BookView.as_view({'get': 'retrieve', 'put': 'update', 'delete': 'destroy'})),
]
views.py
class BookView(ModelViewSet):
queryset = Book.objects.all()
serializer_class = BookSerializer