WinterShiver 2019-06-29
from django.conf.urls import url from . import views urlpatterns = [ url(r'idcs/$',views.idc_list), url(r'idcs/(?P<pk>[0-9]+)/$',views.idc_detail), ]
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"
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)
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装饰器
## 版本二 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<pk>[0-9]+)/$',views.idc_detail_v2,name='idc-detail'), ] urlpatterns=format_suffix_patterns(urlpatterns)
### 版本二 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
###版本三 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<pk>[0-9]+)/$',views.IdcDetailView.as_view,name='idc-detail'), ] urlpatterns=format_suffix_patterns(urlpatterns)
### 版本三 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)
###版本四 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<pk>[0-9]+)/$',views.IdcDetail_V4.as_view(),name='idc-detail'), ] urlpatterns=format_suffix_patterns(urlpatterns)
###版本四 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)
###版本五 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<pk>[0-9]+)/$',views.IdcDetail_V5.as_view(),name='idc-detail'), ] urlpatterns=format_suffix_patterns(urlpatterns)
### 版本五 class IdcList_V5(generics.ListCreateAPIView): queryset = Idc.objects.all() serializer_class = IdcSerializer class IdcDetail_V5(generics.RetrieveUpdateDestroyAPIView): queryset = Idc.objects.all() serializer_class = IdcSerializer
## 版本六 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<pk>[0-9]+)/$',idc_detail,name='idc-detail'), ] urlpatterns=format_suffix_patterns(urlpatterns)
####版本六 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
###版本七 from rest_framework.routers import DefaultRouter router = DefaultRouter() router.register("idcs",views.IdcViewSet_V7) #自动适配,字啊需要加的话就直接加 urlpatterns = [ url(r'^',include(router.urls)) ]
### 版本七 class IdcViewSet_V7(viewsets.ModelViewSet): queryset = Idc.objects.all() serializer_class = IdcSerializer