摘要:将资源连接到视图和的约定可以使用类自动处理。视图视图集之间的权衡使用视图集可以是一个非常有用的抽象。这并不意味着采用视图集总是正确的方法。在使用基于类的视图而不是基于函数的视图时,有一个类似的权衡要考虑。
视图集 简介
REST框架包括一个用于处理ViewSets的抽象,它允许开发人员集中精力对API的状态和交互进行建模,并根据常规约定自动处理URL构造。
ViewSet类与View类几乎相同,不同之处在于它们提供诸如read或update之类的操作,而不是get或put等方法处理程序
ViewSet类只绑定到一组方法处理程序,当它被实例化成一组视图的时候,通常通过使用一个Router类来处理自己定义URL conf的复杂性。
路由器因为我们使用的是ViewSet类而不是View类,我们实际上不需要自己设计URL。将资源连接到视图和url的约定可以使用Router类自动处理。我们需要做的就是使用路由器注册相应的视图集,然后让它执行其余操作。视图vs视图集之间的权衡
使用视图集可以是一个非常有用的抽象。它有助于确保URL约定在你的API中保持一致,最大限度地减少编写所需的代码量,让你能够专注于API提供的交互和表示,而不是URLconf的细节。
这并不意味着采用视图集总是正确的方法。在使用基于类的视图而不是基于函数的视图时,有一个类似的权衡要考虑。使用视图集不像多带带构建视图那样明确。
不同的版本演练 版本一idc/urls.py
from django.conf.urls import url from . import views urlpatterns = [ url(r"idcs/$",views.idc_list), url(r"idcs/(?P[0-9]+)/$",views.idc_detail), ]
models.py
from django.db import models # Create your models here. class Idc(models.Model): name = models.CharField("IDC名称",max_length=128,blank=False,null=True) address = models.CharField("IDC地址",max_length=200,default="") phone = models.CharField("IDC联系电话",max_length=20,null=True) email = models.EmailField("IDC邮箱") def __str__(self): return self.name class Meta: db_table = "idc"
serializers.py
from rest_framework import serializers from .models import Idc class IdcSerializer(serializers.Serializer): id = serializers.IntegerField(read_only=True) name = serializers.CharField(required=False) address = serializers.CharField(required=False) phone = serializers.CharField(required=False) email = serializers.EmailField(required=False) def update(self, instance, validated_data): instance.name = validated_data.get("name",instance.name) instance.address = validated_data.get("address",instance.address) instance.phone = validated_data.get("phone",instance.phone) instance.email = validated_data.get("email",instance.email) instance.save() return instance def create(self, validated_data): return Idc.objects.create(**validated_data)
views.py
from .models import Idc from .serializers import IdcSerializer from rest_framework.renderers import JSONRenderer from rest_framework.parsers import JSONParser from django.http import HttpResponse,JsonResponse class JSONResponse(HttpResponse): def __init__(self,data,**kwargs): kwargs.setdefault("content_type", "application/json") content = JSONRenderer().render(data) super(JSONResponse,self).__init__(content=content,**kwargs) def idc_list(request,*args,**kwargs): if request.method == "GET": queryset = Idc.objects.all() serializer = IdcSerializer(queryset,many=True) return JSONResponse(serializer.data) # content = JSONRenderer().render(serializer.data) # return HttpResponse(content,content_type="application/json") elif request.method == "POST": content = JSONParser().parse(request) serializer = IdcSerializer(data=content) if serializer.is_valid(): serializer.save() content = JSONRenderer().render(serializer.data) return HttpResponse(content, content_type="application/json") def idc_detail(request,pk,*args,**kwargs): try: idc = Idc.objects.get(pk=pk) except Idc.DoesNotExist: return HttpResponse(status=404) if request.method == "GET": serializer = IdcSerializer(idc) return JSONResponse(serializer.data) elif request.method == "POST": content = JSONParser().parse(request) serializer = IdcSerializer(data=content) if serializer.is_valid(): serializer.save() return JSONResponse(serializer.data) elif request.method == "PUT": content = JSONParser().parse(request) serializer = IdcSerializer(idc,data=content) #这里要注意传入instance(idc)否则就是创建操作 if serializer.is_valid(): serializer.save() return JSONResponse(serializer.data) elif request.method == "DELETE": idc.delete() return HttpResponse(status=204)版本二
基于rest_framework下的:Response方法,status定义和api_view装饰器
idc/urls.py
## 版本二 from rest_framework.urlpatterns import format_suffix_patterns urlpatterns = [ url(r"^$",views.api_root), url(r"idcs/$",views.idc_list_v2,name="idc-list"), url(r"idcs/(?P[0-9]+)/$",views.idc_detail_v2,name="idc-detail"), ] urlpatterns=format_suffix_patterns(urlpatterns)
idc/views.py
### 版本二 from rest_framework.response import Response from rest_framework import status from rest_framework.decorators import api_view @api_view(["GET","POST"]) def idc_list_v2(request,*args,**kwargs): if request.method == "GET": queryset = Idc.objects.all() serializer = IdcSerializer(queryset,many=True) return Response(serializer.data) elif request.method == "POST": content = JSONParser().parse(request) serializer = IdcSerializer(data=content) if serializer.is_valid(): serializer.save() return Response(serializer.data,status=status.HTTP_201_CREATED) return Response(serializer.data,status=status.HTTP_400_BAD_REQUEST) @api_view(["GET","POST","PUT","DELETE"]) def idc_detail_v2(request,pk,*args,**kwargs): try: idc = Idc.objects.get(pk=pk) except Idc.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == "GET": serializer = IdcSerializer(idc) return Response(serializer.data) elif request.method == "POST": content = JSONParser().parse(request) serializer = IdcSerializer(data=content) if serializer.is_valid(): serializer.save() return Response(serializer.data) elif request.method == "PUT": content = JSONParser().parse(request) serializer = IdcSerializer(idc,data=content) if serializer.is_valid(): serializer.save() return Response(serializer.data) elif request.method == "DELETE": idc.delete() return Response(status=status.HTTP_204_NO_CONTENT) from rest_framework.reverse import reverse @api_view(["GET"]) def api_root(request,format=None,*args,**kwargs): return Response({ "idcs":reverse("idc-list",request=request,format=format) })
测试:
浏览器打开:http://192.168.33.10:8080/idcs/ 即可
版本三基于类视图的版本,要注意接受提交数据用的是request.data
idc/urls.py
###版本三 from rest_framework.urlpatterns import format_suffix_patterns urlpatterns = [ url(r"^$",views.api_root), url(r"idcs/$",views.IdcListView.as_view,name="idc-list"), url(r"idcs/(?P[0-9]+)/$",views.IdcDetailView.as_view,name="idc-detail"), ] urlpatterns=format_suffix_patterns(urlpatterns)
idc/views.py
### 版本三 from rest_framework.views import APIView from django.http import Http404 class IdcListView(APIView): def get(self,request,format=None): queryset = Idc.objects.all() serializer = IdcSerializer(queryset, many=True) return Response(serializer.data) def post(self,request,format=None): #content = JSONParser().parse(request) serializer = IdcSerializer(data=request.data) #request.data获取请求参数 if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.data, status=status.HTTP_400_BAD_REQUEST) class IdcDetailView(APIView): def get_obj(self,pk): try: return Idc.objects.get(pk=pk) except Idc.DoesNotExist: raise Http404 def get(self,request,pk,format=None): idc = self.get_obj(pk) serializer = IdcSerializer(idc) return Response(serializer.data) def put(self,request,pk,format=None): idc = self.get_obj(pk) serializer = IdcSerializer(idc,data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.data,status=status.HTTP_400_BAD_REQUEST) def delete(self,request,pk,format=None): idc = self.get_obj(pk) idc.delete() return Response(status=status.HTTP_204_NO_CONTENT)版本四
idc/urls.py
###版本四 from rest_framework.urlpatterns import format_suffix_patterns urlpatterns = [ url(r"^$",views.api_root), url(r"idcs/$",views.IdcList_V4.as_view(),name="idc-list"), url(r"idcs/(?P[0-9]+)/$",views.IdcDetail_V4.as_view(),name="idc-detail"), ] urlpatterns=format_suffix_patterns(urlpatterns)
views.py
###版本四 from rest_framework import mixins,generics class IdcList_V4(generics.GenericAPIView, mixins.ListModelMixin, mixins.CreateModelMixin): queryset = Idc.objects.all() serializer_class = IdcSerializer 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 IdcDetail_V4(generics.GenericAPIView, mixins.RetrieveModelMixin, mixins.UpdateModelMixin, mixins.DestroyModelMixin): queryset = Idc.objects.all() serializer_class = IdcSerializer 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)版本五
urls.py
###版本五 from rest_framework.urlpatterns import format_suffix_patterns urlpatterns = [ url(r"^$",views.api_root), url(r"idcs/$",views.IdcList_V5.as_view(),name="idc-list"), url(r"idcs/(?P[0-9]+)/$",views.IdcDetail_V5.as_view(),name="idc-detail"), ] urlpatterns=format_suffix_patterns(urlpatterns)
views.py
### 版本五 class IdcList_V5(generics.ListCreateAPIView): queryset = Idc.objects.all() serializer_class = IdcSerializer class IdcDetail_V5(generics.RetrieveUpdateDestroyAPIView): queryset = Idc.objects.all() serializer_class = IdcSerializer版本六
urls.py
## 版本六 idc_list = views.IdcViewSet.as_view({ "get":"list", "post":"create" }) idc_detail = views.IdcViewSet.as_view({ "get":"retrieve", "put":"update", "delete":"destroy" }) urlpatterns = [ url(r"^$",views.api_root), url(r"idcs/$",idc_list,name="idc-list"), url(r"idcs/(?P[0-9]+)/$",idc_detail,name="idc-detail"), ] urlpatterns=format_suffix_patterns(urlpatterns)
views.py
####版本六 from rest_framework import viewsets class IdcViewSet(viewsets.GenericViewSet, mixins.RetrieveModelMixin, mixins.UpdateModelMixin, mixins.DestroyModelMixin, mixins.ListModelMixin, mixins.CreateModelMixin): queryset = Idc.objects.all() serializer_class = IdcSerializer版本七
urls.py
###版本七 from rest_framework.routers import DefaultRouter router = DefaultRouter() router.register("idcs",views.IdcViewSet_V7) #自动适配,字啊需要加的话就直接加 urlpatterns = [ url(r"^",include(router.urls)) ]
views.py
### 版本七 class IdcViewSet_V7(viewsets.ModelViewSet): queryset = Idc.objects.all() serializer_class = IdcSerializer
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/42740.html
摘要:为什么要进行前后端分离多端适配开发模式的流行前后端职责不清前后端开发效率问题,前后端相互等待前段一直配合后端,能力受到限制后端开发语言和模板高度耦合,导致开发语言依赖严重前后端分离的缺点前后端学习门槛增加数据依赖,导致文档的重要性增加前段工 DRF 为什么要进行前后端分离 PC,APP,PAD多端适配 SPA开发模式的流行 前后端职责不清 前后端开发效率问题,前后端相互等待 前段一直...
摘要:实战小练习需求获取指定组下的列表往用户组添加用户从组中删除用户需求分析获取指定组下的列表网用户组添加用户从组中删除用户走界面不行项目结构主路由配置文件开源运维平台子路由配置文件序列 实战小练习 需求 users userGroups groups groupUsers---- get : 获取指定组下的user列表-----put:往用户组添加用户-----delete:从组中删除用...
摘要:后端实现跨域第三方扩展在介绍之前,我先介绍两个概念同源策略跨域同源策略同源策略是一种约定,是浏览器的一种安全机制。这里同源需要协议域名端口三者都相同,否则不能进行访问。跨域不同源之间的网站通信就是跨域。 在使用django-rest-framework开发项目的时候我们总是避免不了跨域的问题,因为现在大多数的项目都是前后端分离,前后端项目部署在不同的web服务器上,因为我们是后端程序员...
阅读 1845·2021-11-25 09:43
阅读 3687·2021-11-24 10:32
阅读 1075·2021-10-13 09:39
阅读 2327·2021-09-10 11:24
阅读 3343·2021-07-25 21:37
阅读 3463·2019-08-30 15:56
阅读 857·2019-08-30 15:44
阅读 1447·2019-08-30 13:18