- 一、基本路由(原始方式)
- 二、半自动路由(视图继承ModelViewSet)
- 三、自动路由(自动生成路由)
准备工作:
models中创建一张表,默认使用sqlite数据库,更新表
新增测试数据
加载rest_framework应用
方式一:
手动设置路由:
编辑视图:
1,加载模块
2、设置视图
设置路由:
创建一个文件:app01Serializer(名字自定义)
# app01Serializer.py from rest_framework import serializers from app01 import models 调用表publish的所有字段 class PublishSerializers(serializers.ModelSerializer): class Meta: # 调用所有数据 model=models.Publish fields='__all__'
# 设置前端显示那些字段
# fields=('id','name')
# 前端不显示那些字段
# exclude=('id',)
# 当表有关联的时候可以添加深度
# depth =1
编辑视图:
写get获取数据和post数据方法
# views.py from rest_framework.views import APIView from rest_framework.response import Response from app01 import models # 引用上个文件中的BookSerializer类 from app01.app01Serializer import BookSerializer class Books(APIView): def get(self,request): books = models.Book.objects.all() # 当序列化一条数据的时候many=True可以不写,序列化多条数据的时候(也就是queryset对象)必须要写 bookser=BookSerializer(books,many=True) print(type(bookser.data)) return Response(bookser.data)
def post(self,request): bookser=BookSerializer(data=request.data) if bookser.is_valid(): bookser.save() return Response()
页面访问:
post数据:
注意:查询数据的时候只能查询所有数据,不能查询单个数据
想要可以查询单个数据需要重新定义路由,重新写对应的视图
单独对数据进行查询新增删除的借口:
url(r'publish/(?P\d+)/$',views.PublishOnlyView.as_view()),
编辑视图:
class PublishOnlyView(APIView): def get(self,request,pk): publish_list = models.Publish.objects.filter(pk=pk).first() ps=PublishSerializers(publish_list,many=False) return Response(ps.data)
def post(self,request,pk):
publish_list = models.Publish.objects.filter(pk=pk).first()
ps = PublishSerializers(data=request.data,instance=publish_list)
if ps.is_valid():
ps.save()
return Response(ps.data)
else:
return Response(ps.errors)
def delete(self,request,pk):
models.Publish.objects.filter(pk=pk).delete()
return Response("")
第二种:mixin类和generice类编写视图
路由:
url(r'^publish/$',views.PublishView.as_view()), url(r'publish/(?P\d+)/$ ',views.PublishDetailView.as_view()),
视图:
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)
实现效果:
查询所有数据:可以新增数据,不需要通过json方式新增,可以通过输入框新增数据
单条数据:查询,修改,修改数据中会在输入框中显示原数据,删除数据
第三种:使用generics 下ListCreateAPIView,RetrieveUpdateDestroyAPIView
url:
url(r'^publish/$',views.PublishView.as_view()), url(r'publish/(?P\d+)/$ ',views.PublishDetailView.as_view()),
视图:
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(根据需要添加功能):
url(r'^publish/$', views.PublishView.as_view({'get': 'list', 'post': 'create'})), url(r'^publish/(?P\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