django, rest类视图与mixins

一. 类视图

二. mixins

三.更加精简的通用类视图


一. 类视图

类视图与from rest_framework.views import APIView继承与from django.views.generic import View, 使用上大致相同

  • 一小段代码


class UserDetail(APIView): def get_user(self, pk): # 查看 user = Usertest.objects.filter(pk=pk) if not user: raise Http404 else: return user[0] def get(self, request, pk, format=None): user = self.get_user(pk) user_serializer = UsertestSerializer1(user) return Response(user_serializer.data) def put(self, request, pk, format=None): # 修改 user = self.get_user(pk) user_serializer = UsertestSerializer1(user, data=request.data) if user_serializer.is_valid(): user_serializer.save() return Response(user_serializer.data) return Response(user_serializer.errors, status=status.HTTP_400_BAD_REQUEST) def delete(self, request, pk, format=None): # 删除 user = self.get_user(pk) user.delete() return Response(stauts=status.HTTP_204_NO_CONTENT) class UserList(APIView): def get(self, request, format=None): # 查看所有 users = Usertest.objects.all() users_serializer = UsertestSerializer1(users, many=True) return Response(users_serializer.data) def post(self, request, format=None): # 创建 user_data = request.data user_serializer = UsertestSerializer1(data=user_data) if user_serializer.is_valid(): user_serializer.save() return Response(user_serializer.data) return Response(status=status.HTTP_400_BAD_REQUEST)

同样的url也应该与之对应


from django.conf.urls import url from restapp import views from rest_framework.urlpatterns import format_suffix_patterns urlpatterns = [ url(r'^user_list/$', views.UserList.as_view()), url(r'^user_detail/(?P<pk>\d+)/$', views.UserDetail.as_view()), ] urlpatterns = format_suffix_patterns(urlpatterns)

二. mixins

增删改查等常见操作已经被封装在了REST framework的mixins类中, 因此可以继续简化views.py代码


from rest_framework import mixins from rest_framework import generics from restapp.models import Usertest from restapp.serializers import UsertestSerializer1 # 注意除了继承必要的操作mixin外还需要继承generics.GenericAPIView class UserList(mixins.ListModelMixin, mixins.CreateModelMixin, generics.GenericAPIView): """ 获取和创建 """ # 注意这两个类属性 queryset = Usertest.objects.all() serializer_class = UsertestSerializer1 # get操作 def get(self, request, *args, **kwargs): # 直接调用list即可 return self.list(request, *args, **kwargs) def put(self, request, *args, **kwargs): return self.create(request, *args, **kwargs) class UserDetail(mixins.RetrieveModelMixin, mixins.UpdateModelMixin, mixins.DestroyModelMixin, generics.GenericAPIView): """ 查看, 更新和删除 """ queryset = Usertest.objects.all() serializer_class = UsertestSerializer1 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, *args, **kwargs)

三. 通用类视图的精简自己体会

  • 完整代码


from rest_framework import generics from restapp.models import Usertest from restapp.serializers import UsertestSerializer1 class UserDetail(generics.ListCreateAPIView): queryset = Usertest.objects.all() serializer_class = UsertestSerializer1 class UserList(generics.RetrieveUpdateDestroyAPIView): queryset = Usertest.objects.all() serializer_class = UsertestSerializer1