圆月山庄资源网 Design By www.vgjia.com
一、基本视图
写一个出版社的增删查改resful接口
要自己事先创建好django项目,并创建好表,添加完记录
路由:
url(r'^publish/$', views.PublishView.as_view()),
url(r'^publish/("htmlcode">from api import modelsfrom rest_framework import serializersfrom rest_framework.response import Responsefrom rest_framework.views import APIView 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("")二、mixins类和generic类编写视图
路由:
url(r'^publishes$', views.Publish.as_view()),
url(r'^publish/("htmlcode">from api import models from rest_framework import serializers from rest_framework import mixins from rest_framework.generics import GenericAPIView class PublishSer(serializers.ModelSerializer): class Meta: model=models.Publish fields='__all__' class Publish(GenericAPIView,mixins.ListModelMixin,mixins.CreateModelMixin): queryset = models.Publish.objects.all() serializer_class = PublishSer def get(self,request): return self.list(request) #在ListModelMixin下有一个list方法,返回的就是查询的结果:Response(serializer.data) def post(self,request): return self.create(request) #已经封装好了,帮我们做校验和序列化,返回的是添加成功的记录 class PublishDetail(GenericAPIView,mixins.DestroyModelMixin,mixins.RetrieveModelMixin,mixins.UpdateModelMixin): queryset = models.Publish.objects.all() serializer_class = PublishSer 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)三、使用generic下ListCreateAPIView和RetrieveUpdateDestroyAPIView
路由:
url(r'^publishes$', views.Publish.as_view()),
url(r'^publish/("htmlcode">from api import models from rest_framework import mixins from rest_framework.generics import GenericAPIView from rest_framework.generics import ListCreateAPIView,RetrieveUpdateDestroyAPIView from rest_framework import serializers class Publishser(serializers.ModelSerializer): class Meta: model=models.Publish fields='__all__' class Publish(ListCreateAPIView): queryset = models.Publish.objects.all() serializer_class = Publishser class PublishDetail(RetrieveUpdateDestroyAPIView): queryset = models.Publish.objects.all() serializer_class = Publishser四、使用ModelViewSet
路由:
url(r'^publish$',views.Publish.as_view({'get':'list','post':'create'})),
url(r'^publish/("htmlcode">from rest_framework.viewsets import ModelViewSet from api import models from rest_framework import serializers class Publishser(serializers.ModelSerializer): class Meta: model=models.Publish fields='__all__' class Publish(ModelViewSet): queryset = models.Publish.objects.all() serializer_class = Publishser以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com
暂无评论...
更新日志
2024年11月01日
2024年11月01日
- 《暗喻幻想》顺风耳作用介绍
- 崔健1985-梦中的倾诉[再版][WAV+CUE]
- 黄子馨《追星Xin的恋人们2》HQ头版限量编号[WAV+CUE]
- 孟庭苇《情人的眼泪》开盘母带[低速原抓WAV+CUE]
- 孙露《谁为我停留HQCD》[低速原抓WAV+CUE][1.1G]
- 孙悦《时光音乐会》纯银CD[低速原抓WAV+CUE][1.1G]
- 任然《渐晚》[FLAC/分轨][72.32MB]
- 英雄联盟新英雄安蓓萨上线了吗 新英雄安蓓萨技能介绍
- 魔兽世界奥杜尔竞速赛什么时候开启 奥杜尔竞速赛开启时间介绍
- 无畏契约CGRS准星代码多少 CGRS准星代码分享一览
- 张靓颖.2012-倾听【少城时代】【WAV+CUE】
- 游鸿明.1999-五月的雪【大宇国际】【WAV+CUE】
- 曹方.2005-遇见我【钛友文化】【WAV+CUE】
- Unity6引擎上线:稳定性提升、CPU性能最高提升4倍
- 人皇Sky今日举行婚礼!电竞传奇步入新篇章