restframeword之视图,解析器
最终版本 五个方法封装
位置参数 看源码
视图四个方法:
视图
(1) (常用)
class AuthorSerializers(serializers.ModelSerializer): class Meta: model=Author fields="__all__" class AuthorView(APIView): def get(): pass def post(): pass class AuthorDetailView(APIView): def get(): pass def delete(): pass def put(): pass
from rest_framework.mixins import CreateModelMixin,ListModelMixin,DestroyModelMixin,RetrieveModelMixin,UpdateModelMixin
from rest_framework import generics
(2) class AuthorView(CreateModelMixin,ListModelMixin,generics.GenericAPIView,):
queryset=Author.objects.all serializer_class=AuthorSerializers def get(self,request): return self.list(request) def post(self,request): return self.create(request) class AuthorDetailView(DestroyModelMixin,UpdateModelMixin,RetrieveModelMixin,generics.GenericAPIView,): queryset = Author.objects.all serializer_class = AuthorSerializers 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)
(3) class AuthorView(generics.ListCreateAPIView):
class AuthorView(generics.ListCreateAPIView): queryset=Author.objects.all serializer_class=AuthorSerializers class AuthorDetailView(generics.RetrieveUpdateDestroyAPIView): queryset = Author.objects.all serializer_class = AuthorSerializers
from rest_framework.viewsets import ModelViewSet
(4) class AuthorModelView(ModelViewSet):(常用)
class AuthorModelView(ModelViewSet) queryset=Author.objects.all() serializer_class=AuthorSerializers
解析器
如果一个属性点一个没有的属性
就会执行这个
可以定义全局
解析器是什么时候执行,request.data
解析器
class APIView:
parser_classes = api_settings.DEFAULT_PARSER_CLASSES