161.Django-restframe基于视图类使用mixins实现最简易的增删改查

1. 概述

使用CBV的优势就是可以轻松的编写可重用的行为,在rest_framework中使用mixins,可以实现通用的创建、查询、更新、删除等操作

2.代码展示

views

from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt
from rest_app.models import *
from rest_app.app_serializer import StudentSerializer,ClassesSerializer
from django.http import JsonResponse,HttpResponse,Http404
from rest_framework.parsers import JSONParser
from rest_framework.request import Request
from rest_framework.response import Response
from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.views import APIView
from rest_framework import mixins, generics
# Create your views here.
'''
    新增        post    students/
    删除        delete  students/id/
    修改        put     students/id/
    查询一个    get     students/id/
    查询所有    get     students/
'''
# 优化代码:
class StudentsView(generics.ListCreateAPIView):
    # 指定需要操作的数据与序列化类
    queryset = Student.objects.all()
    serializer_class = StudentSerializer

class StudentDetailView(generics.RetrieveUpdateDestroyAPIView):
    queryset = Student.objects.all()
    serializer_class = StudentSerializer

# 查看generics源码后发现,里面存在了我们下面编写的代码,我们只需要直接继承类就好
# class StudentsView(mixins.ListModelMixin,       # 查询
#                    mixins.CreateModelMixin,     # 增加
#                    generics.GenericAPIView):    # 用于指定参数queryset\serializer_class
#     # 指定需要操作的数据与序列化类
#     queryset = Student.objects.all()
#     serializer_class = StudentSerializer

#     # 查询
#     def get(self, request, *args, **kwargs):
#         return self.list(request, *args, **kwargs) # 传递参数
#     # 增加
#     def post(self, request, *args, **kwargs):    
#         return self.create(request, *args, **kwargs)


# class StudentDetailView(mixins.RetrieveModelMixin, # 根据id查询单个实体
#                         mixins.UpdateModelMixin,   # 修改
#                         mixins.DestroyModelMixin,  # 删除
#                         generics.GenericAPIView):  # 用于指定参数queryset\serializer_class
#     # 指定需要操作的数据与序列化类
#     queryset = Student.objects.all()
#     serializer_class = StudentSerializer
#     # 指定参数查询
#     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)

urls

from django.contrib import admin
from django.urls import path,include
from . import views
urlpatterns = [
    # path('students/',views.students),
    # path('students//',views.students_detail),
    # path('classes/',views.classes),
    # path('classes//',views.classes_detail),
    path('students/',views.StudentsView.as_view()),
    path('students//',views.StudentDetailView.as_view())
]
from rest_framework.urlpatterns import format_suffix_patterns
# 可以使用allowed = ['json','html'] 参数指定允许的后缀
urlpatterns = format_suffix_patterns(urlpatterns)

3.效果展示

161.Django-restframe基于视图类使用mixins实现最简易的增删改查_第1张图片

你可能感兴趣的:(Python_Django框架,django,python,后端,web,数据库)