rest-framework之视图
一 、基本视图
写一个出版社的增删查改resful接口
url:
url(r'^publish/$', views.PublishView.as_view()),
url(r'^publish/(?P<pk>d+)/$', views.PublishDetailView.as_view()),
views:
class PublishSerializers(serializers.ModelSerializer):
class Meta:
model=models.Publish
fields='__all__'
class PublishView(APIView):
def get(self, request):
publish_list = models.Publish.objects.all()
bs = PublishSerializers(publish_list, many=True)
# 序列化数据
return Response(bs.data)
def post(self, request):
# 添加一条数据
print(request.data)
bs=PublishSerializers(data=request.data)
if bs.is_valid():
bs.save() # 生成记录
return Response(bs.data)
else:
return Response(bs.errors)
class PublishDetailView(APIView):
def get(self,request,pk):
publish_obj=models.Publish.objects.filter(pk=pk).first()
bs=PublishSerializers(publish_obj,many=False)
return Response(bs.data)
def put(self,request,pk):
publish_obj = models.Publish.objects.filter(pk=pk).first()
bs=PublishSerializers(data=request.data,instance=publish_obj)
if bs.is_valid():
bs.save() # update
return Response(bs.data)
else:
return Response(bs.errors)
def delete(self,request,pk):
models.Publish.objects.filter(pk=pk).delete()
return Response("")
二 、mixin类和generice类编写视图
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 post(self,request,*args, **kwargs):
return self.create(request,*args, **kwargs)
def get(self,request,*args, **kwargs):
return self.list(request,*args, **kwargs)
class BookView(RetrieveModelMixin,UpdateModelMixin,DestroyModelMixin,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)
class PublishView(CreateModelMixin,GenericAPIView):
serializer_class=PublishSerializer
def post(self,request,*args, **kwargs):
return self.create(request,*args, **kwargs)
三 、使用generics 下ListCreateAPIView,RetrieveUpdateDestroyAPIView
from rest_framework.generics import ListAPIView,ListCreateAPIView,RetrieveUpdateDestroyAPIView,DestroyAPIView,CreateAPIView
#它有两个接口,一个是获取所有,一个是新增
class BooksView(ListCreateAPIView):
serializer_class=BookSerializer
queryset=models.Book.objects.all()
class BookView(RetrieveUpdateDestroyAPIView):
serializer_class=BookSerializer
queryset=models.Book.objects.all()
四 、使用ModelViewSet
url:
url(r'^publish/$', views.PublishView.as_view({'get':'list','post':'create'})),
url(r'^publish/(?P<pk>d+)/$', views.PublishView.as_view({'get':'retrieve','put':'update','delete':'destroy'})),
views
from rest_framework.viewsets import ModelViewSet
class BooksView(ModelViewSet):
queryset=models.Book.objects.all()
serializer_class=BookSerializer
五、ViewSetMixin的使用,重新了as_view方法
-要用ViewSetMixin,路由跟之前写法不一样了
-url(r'^test', views.Publish.as_view({'get':'aa'})),
-视图类中
from rest_framework.viewsets import ViewSetMixin
class Publish(ViewSetMixin,APIView):
def aa(self,request):
return HttpResponse('aa')