Restful_framework视图组件代码实例解析
一、基本视图
写一个出版社的增删查改resful接口
要自己事先创建好django项目,并创建好表,添加完记录
路由:
url(r'^publish/$', views.publishview.as_view()),
url(r'^publish/(?p<pk>\d+)/$', views.publishdetailview.as_view()),
视图:
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/(?p<pk>\d+)', views.publishdetail.as_view()),
视图:
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/(?p<pk>\d+)', views.publishdetail.as_view()),
视图:
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/(?p<pk>\d+)', views.publish.as_view({'get': 'retrieve', 'put': 'update', 'delete': 'destroy'})),
视图:
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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 关羽的大刀什么时候改名青龙偃月刀的?