一、 资源的增删改查
1. 新增资源
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
@api_view(['GET', 'POST']) def request_list(request, format=None): if request.method == 'GET': serializer = RequestSerializer(Request.objects.all(), many=True) return Response(serializer.data) elif request.method == 'POST': serializer = RequestSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
2. 修改资源
修改使用的是PUT方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
@api_view(['GET', 'PUT','DELETE']) def request_detail(request, _id, format=None): try: req_obj = Request.objects.get(id=_id) except Exception: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': serializer = RequestSerializer(req_obj) return Response(serializer.data) elif request.method == 'PUT': serializer = RequestSerializer(req_obj, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
3. 删除数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
@api_view(['GET', 'PUT','DELETE']) def request_detail(request, _id, format=None): try: req_obj = Request.objects.get(id=_id) except Exception: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': serializer = RequestSerializer(req_obj) return Response(serializer.data) elif request.method == 'PUT': serializer = RequestSerializer(req_obj, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) elif request.method == 'DELETE': req_obj.delete() return Response(status=status.HTTP_204_NO_CONTENT)
|
二、 基于类的视图
1、 用类重写视图(继承REST框架的APIView类 )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
from rest_framework.views import APIView
class RequestList(APIView): """ 列出所有的 request 数据 或者创建一个新的 request """
def get(self, request, format=None): serializer = RequestSerializer(Request.objects.all(), many=True) return Response(serializer.data)
def post(self, request, format=None): serializer = RequestSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
以上代码完成了查询列表和新增单个数据的功能 ,新增了get(处理get请求)和post(处理post请求)方法,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
class RequestDetail(APIView):
def get(self, request, _id, format=None): req_obj = Request.objects.get(id=_id) serializer = RequestSerializer(req_obj) return Response(serializer.data)
def put(self, request, _id, format=None): req_obj = Request.objects.get(id=_id) serializer = RequestSerializer(req_obj, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def delete(self, request, _id, format=None): req_obj = Request.objects.get(id=_id) req_obj.delete() return Response(status=status.HTTP_204_NO_CONTENT)
|
启动服务之前,修改下sqpt/urls.py
,此时我们使用的是基于类的视图
1 2 3 4 5 6 7 8 9 10 11 12 13
|
from django.urls import path from sqpt import views from rest_framework.urlpatterns import format_suffix_patterns urlpatterns = [ path('requests/', views.RequestList.as_view()), path('requests/<int:_id>', views.RequestDetail.as_view()), ]
urlpatterns = format_suffix_patterns(urlpatterns)
|
2. 通用类视图 - 1
generics.ListCreateAPIView
:提供 列出所有 和 创建数据 功能
generics.RetrieveUpdateDestroyAPIView
:提供 查询单个 、修改 和 删除数据 功能
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
class RequestList(generics.ListCreateAPIView): """ 列出所有的 request 数据 或者创建一个新的 request """ queryset = Request.objects.all() serializer_class = RequestSerializer
class RequestDetail(generics.RetrieveUpdateDestroyAPIView): """ 查询单个 request 数据 ,修改 或者 删除 """ queryset = Request.objects.all() serializer_class = RequestSerializer
|
修改路由:
1 2 3
|
path('requests/<int:pk>', views.RequestDetail.as_view()),
|
三、 视图集 ViewSet(重点)
1. ViewSet(视图集)
ViewSet(视图集)
代替View类重构视图
1 2 3 4 5 6 7 8 9 10 11 12
|
class RequestViewSet(viewsets.ModelViewSet): """ 列出所有的 request 数据 或者创建一个新的 request 查询单个 request 数据 ,修改 或者 删除 """ queryset = Request.objects.all() serializer_class = RequestSerializer
|
一个 ViewSet 类只绑定到一组方法处理程序,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| class StepViewSet(viewsets.ModelViewSet): queryset = Step.objects.all() serializer_class = StepSerializer
class ConfigViewSet(viewsets.ModelViewSet): queryset = Config.objects.all() serializer_class = ConfigSerializer
class CaseViewSet(viewsets.ModelViewSet): queryset = Case.objects.all() serializer_class = CaseSerializer
class SuiteViewSet(viewsets.ModelViewSet): queryset = Suite.objects.all() serializer_class = SuiteSerializer
|
当它被实例化成一组视图的时候,通常通过使用一个Router 类来代替自己定义复杂的URL。
2. 路由设计的自动化
利用rest框架的router,可以帮助我们自动生成路由列表。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
from django.urls import path, include from sqpt import views
from rest_framework.routers import DefaultRouter
router = DefaultRouter()
router.register(r'requests', views.RequestViewSet) router.register(r'steps', views.StepViewSet) router.register(r'suites', views.SuiteViewSet) router.register(r'configs', views.ConfigViewSet) router.register(r'cases', views.CaseViewSet) urlpatterns = [ path('', include(router.urls)), ]
|
router的作用是根据你注册的路由前缀,帮助你自动生成诸如此类的路由列表
1 2 3 4
| ^requests/$ [name='request-list'] ^requests\.(?P<format>[a-z0-9]+)/?$ [name='request-list'] ^requests/(?P<pk>[^/.]+)/$ [name='request-detail'] ^requests/(?P<pk>[^/.]+)\.(?P<format>[a-z0-9]+)/?$ [name='request-detail']
|