rest-framework-----视图
2024-09-26 14:35:36
一:基本视图
写一个出版社的增删改查的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.mixins import CreateModelMixin,RetrieveModelMixin,ListModelMixin,UpdateModelMixin,DestroyModelMixin
from rest_framework.generics import GenericAPIView
class PublishView(ListModelMixin,CreateModelMixin,GenericAPIView):
queryset=models.Publish.objects.all()
serializer_class=PublishSerializers def get(self, request):
return self.list(request) def post(self, request):
return self.create(request) class PublishDetailView(RetrieveModelMixin,UpdateModelMixin,DestroyModelMixin,GenericAPIView):
queryset=models.Publish.objects.all()
serializer_class=PublishSerializers
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)
三:使用generics
from rest_framework.generics import ListCreateAPIView,RetrieveUpdateDestroyAPIView
class PublishView(ListCreateAPIView):
queryset=models.Publish.objects.all()
serializer_class=PublishSerializers class PublishDetailView(RetrieveUpdateDestroyAPIView):
queryset=models.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=models.Publish.objects.all()
serializer_class=PublishSerializers
最新文章
- 卷积神经网络_(4)_caffe简介
- NPOI格式设置1
- [问题2014S10] 复旦高等代数II(13级)每周一题(第十教学周)
- 数组添加:如何往数组的";null";位置插入数据呢?
- Hbase之遍历超时处理
- python获取外网地址
- ASP.NET Web Api返回对象类型为JSON还是XML
- Log4J积累
- netcat工具的使用
- AppBoxFuture(二): Say goodbye to sql!
- Chrome Inspect调试微信出现空白页面的解决方法
- Javascript 2.9
- git-bisect last updated in 2.19.1【转】
- UUID生成字符串
- C# 拼接字符串的几种方式和性能
- Python学习笔记4-os,sys模块
- 3D旋转相册的实现
- HMM代码实践
- 第6课 仿Siri机器人-语音朗读和语音识别
- gogs配置及迁移