一 基本视图
写一个出版社的增删查改resful接口
路由:
url(r‘^publish/$‘, views.PublishView.as_view()), url(r‘^publish/(?P<pk>\d+)/$‘, views.PublishDetailView.as_view()),
视图:
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.generics import GenericAPIView from rest_framework.mixins import ListModelMixin, CreateModelMixin,RetrieveModelMixin, UpdateModelMixin, DestroyModelMixin
class PublishView(GenericAPIView, ListModelMixin, CreateModelMixin): queryset = Publish.objects.all() serializer_class = PublishSerializers def get(self, request): return self.list(request) def post(self, request): # 添加一条数据 return self.create(request)
三 使用generics 下ListCreateAPIView,RetrieveUpdateDestroyAPIView
from rest_framework.generics import ListCreateAPIView class PublishView(ListCreateAPIView): queryset = Publish.objects.all() serializer_class = PublishSerializers
四 使用ModelViewSet
路由: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‘})), from rest_framework.viewsets import ModelViewSet class PublishView(ModelViewSet): queryset = Publish.objects.all() serializer_class = PublishSerializers
五 ViewSetMixin
用了它之后,视图类中不需要写get,post,put方法了,自己定义方法就可以了,让请求方法对应到自己定义的方法上,配置路由
from django.conf import settingsfrom rest_framework.viewsets import ViewSetMixin
# 注意先后顺序,ViewSetMixin写在前面 class TestAll(ViewSetMixin, APIView): def test(self, request): print(settings.DEBUG) return HttpResponse(‘test‘) def test2(self, request): return HttpResponse(‘test2‘) def test3(self, request): return HttpResponse(‘test3‘)路由
url(r‘^test1/$‘, views.TestAll.as_view({‘get‘: ‘test‘})),url(r‘^test2/$‘, views.TestAll.as_view({‘get‘: ‘test2‘})),url(r‘^test3/$‘, views.TestAll.as_view({‘get‘: ‘test3‘})),
路由控制
三种: -一种基本路由: url(r‘^publish/$‘, views.PublishView.as_view()), -半自动路径: views.PublishView.as_view({‘get‘: ‘list‘, ‘post‘: ‘create‘})) -必须继承: ViewSetMixin - 继承ViewSetMixin的先后顺序 - 全自动路由:(基本不用) - from rest_framework import routers # 生成一个router对象 router = routers.DefaultRouter() # 需要传两个参数,第一个参数就是匹配的路径,第二个参数,是视图类 router.register(‘publish‘, views.PublishView) url(‘‘, include(router.urls)), # 自动生成四个路由(PublishView必须继承ModelViewSet)
响应器:(基本上不需要配置)
局部配置 视图类中: -renderer_classes=[JSONRenderer,BrowsableAPIRenderer] 全局配置 在setting中: REST_FRAMEWORK = { ‘DEFAULT_RENDERER_CLASSES‘:[‘rest_framework.renderers.JSONRenderer‘] }
原文地址:https://www.cnblogs.com/liweiwei0307/p/10133639.html
时间: 2024-10-31 08:06:18