1.使用mixin和generics类编写视图
from rest_framework.mixins import CreateModelMixin, ListModelMixin, DestroyModelMixin, RetrieveModelMixin, UpdateModelMixin from rest_framework.generics import GenericAPIView class BooksView(CreateModelMixin, ListModelMixin, GenericAPIView): serializer_class = BookSerializer queryset = models.Book.objects.all() def get(self, request, *args, **kwargs): return self.list(request, *args, **kwargs) def post(self, request, *args, **kwargs): return self.create(request, *args, **kwargs) class BookView(UpdateModelMixin, DestroyModelMixin, RetrieveModelMixin, GenericAPIView): serializer_class = BookSerializer queryset = models.Book.objects.all() 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)
2.使用generics 下ListCreateAPIView,RetrieveUpdateDestroyAPIView 编写视图
from rest_framework.generics import ListCreateAPIView, RetrieveUpdateDestroyAPIView class BooksView(ListCreateAPIView): queryset = models.Book.objects.all() serializer_class = BookSerializer class BookView(RetrieveUpdateDestroyAPIView): queryset = models.Book.objects.all() serializer_class = BookSerializer
3. 使用ModelViewSet 编写视图
url配置: url(r'^books/(?P<pk>d+)', views.BooksView.as_view({'get': 'retrieve', 'put': 'update', 'delete': 'destroy'})), url(r'^books/$', views.BooksView.as_view({'get': 'list', 'post': 'create'})), views层: # 使用ModelViewSet from rest_framework.viewsets import ModelViewSet class BooksView(ModelViewSet): queryset = models.Book.objects.all() serializer_class = BookSerializer
4.ViewSetMixin的使用
url 配置 url(r'^test/', views.Publish.as_view({ 'get': 'get_all', 'post': 'create_one', 'put': 'update', 'delete': 'delete' })), views: from rest_framework.viewsets import ViewSetMixin class Publish(ViewSetMixin, APIView): def get_all(self, request): return HttpResponse('get_all') def create_one(self, request): return HttpResponse('create_one') def update(self, request): return HttpResponse('update') def delete(self, request): return HttpResponse('delete')
5. 认证组件
drf认证组件源码实现
''' 1: APIView的dispach方法 2.self.initial(request, *args, **kwargs)方法 3. self.perform_authentication(request)[认证] 4.def perform_authentication(self, request) 5. request.user-> Request(request为dispach中替换过的新的request) 6.Request中找到user 7.self._authenticate()[self是新的request对象] 8.Request类的_authenticate(self)方法,深度 '''
from rest_framework.views import APIView from rest_framework.exceptions import AuthenticationFailed import uuid from rest_framework.request import Request class CheckLogin(): def authenticate(self, request): token = request.GET.get('token') ret = models.UserToken.objects.filter(token=token).first() if ret: return None else: raise AuthenticationFailed('验证失败') def authenticate_header(self, request): pass class BooksView(APIView): authentication_classes = [CheckLogin] def get(self, request): return HttpResponse('ok') class Login(APIView): def post(self, request): response = { 'status': 100, 'msg': None } name = request.data.get('name') pwd = request.data.get('pwd') # 数据库校验该用户是否存在 user = models.User.objects.filter(name=name, pwd=pwd).first() if user: # 正常返回登录成功, 返回唯一token token = uuid.uuid4() ret = models.UserToken.objects.update_or_create(user=user, defaults={'token': token}) response['msg'] = '登录成功' response['token'] = token else: response['msg'] = '登录失败' response['status'] = 101 return Response(response)