django之drf框架(两个视图基类、5个扩展视图类、9个视图子类)

两个视图基类

APIView和GenericAPIView

drf提供的最顶层的父类就是APIView,以后所有的类都继承自他

GenericAPIView继承自APIView,他里面封装了一些工能

基于APIView+ModelSerializer+Resposne写5个接口

子路由:app01>>>urls.py

python 复制代码
from django.urls import path, include
from . import views


urlpatterns = [
    path('books/', views.BookView.as_view()),
    path('books/<int:pk>', views.BookDetailView.as_view()),
    path('publish/', views.PublishView.as_view()),
    path('publish/<int:pk>', views.PublishDetailView.as_view()),
]


主路由:
from django.contrib import admin
from django.urls import path, include


urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/v1/', include('app01.urls')),
]

序列化类:

python 复制代码
class BookSerializer(serializers.ModelSerializer):
    class Meta:
        model = Book
        fields = ['id', 'name', 'price', 'publish', 'authors', 'publish_detail', 'author_list']
        extra_kwargs = {
            'publish': {'write_only': True},
            'authors': {'write_only': True},
            'publish_detail': {'read_only': True},
            'author_list': {'read_only': True}
        }

模型表:

python 复制代码
from django.db import models


class Book(models.Model):
    name = models.CharField(max_length=32)
    price = models.DecimalField(max_digits=5, decimal_places=2)

    publish = models.ForeignKey(to='Publish', on_delete=models.CASCADE)
    authors = models.ManyToManyField(to='Author')

    @property
    def publish_detail(self):
        return {'name': self.publish.name, 'city': self.publish.city}

    @property
    def author_list(self):
        l = []
        for author in self.authors.all():
            l.append({'name': author.name, 'age': author.age})
        return l

    def __str__(self):
        return self.name


class Author(models.Model):
    name = models.CharField(max_length=32)
    age = models.IntegerField()
    author_detail = models.OneToOneField(to='AuthorDetail', on_delete=models.CASCADE)

    def __str__(self):
        return self.name


class AuthorDetail(models.Model):
    telephone = models.BigIntegerField()
    birthday = models.DateField()
    addr = models.CharField(max_length=64)


class Publish(models.Model):
    name = models.CharField(max_length=32)
    city = models.CharField(max_length=32)
    email = models.EmailField()

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '出版社'
        verbose_name_plural = verbose_name

视图类:

python 复制代码
# 第一层:继承APIView+序列化类+Response写接口
class BookView(APIView):
    def get(self, request):
        book_list = Book.objects.all()
        ser = BookSerializer(instance=book_list, many=True)
        return Response(ser.data)

    def post(self, request):
        ser = BookSerializer(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response(ser.data)
        else:
            return Response(ser.errors)


class BookDetailView(APIView):
    def put(self,request,*args,**kwargs):
        book = Book.objects.filter(pk=kwargs.get('pk')).first()
        ser = BookSerializer(instance=book,data=request.data)
        if ser.is_valid():
            ser.save()
            return Response(ser.data)
        else:
            return Response(ser.errors)

    def get(self,request,*args,**kwargs):
        book = Book.objects.filter(pk=kwargs.get('pk')).first()
        ser = BookSerializer(instance=book)
        return Response(ser.data)

    def delete(self,request,*args,**kwargs):
        Book.objects.filter(pk=kwargs.get('pk')).delete()
        return Response('')

继承GenericAPIView,编写五个接口

python 复制代码
class BookView(GenericAPIView):
    # 配置两个类属性
    queryset = Book.objects.all()
    serializer_class = BookSerializer

    def get(self, request):
        obj_list = self.get_queryset()
        ser = self.get_serializer(instance=obj_list, many=True)
        return Response(ser.data)

    def post(self, request):
        ser = self.get_serializer(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response(ser.data)
        else:
            return Response(ser.errors)


class BookDetailView(GenericAPIView):
    queryset = Book.objects.all()
    serializer_class = BookSerializer

    def put(self, request, *args, **kwargs):
        # book = Book.objects.filter(pk=kwargs.get('pk')).first()
        obj = self.get_object()  # 获取单条数据
        ser = self.get_serializer(instance=obj, data=request.data)
        if ser.is_valid():
            ser.save()
            return Response(ser.data)
        else:
            return Response(ser.errors)

    def get(self, request, *args, **kwargs):
        obj = self.get_object()
        ser = self.get_serializer(instance=obj)
        return Response(ser.data)

    def delete(self, request, *args, **kwargs):
        self.get_object().delete()
        return Response('')

继承GenericAPIView+序列化类+Response写接口

python 复制代码
#1  继承GenericAPIView的写法
	-1 在类中,写两个类属性:所有数据,序列化类
        queryset = Book.objects.all()
    	serializer_class = BookSerializer
    -2 获取所有要序列化的数据
    	self.get_queryset()
    -3 获取序列化类
    	self.get_serializer(参数跟之前一样)
    -4 获取单挑
    	self.get_object()
        
        
# 2 如果想快速写出Publish的5个接口,只需要修改视图类上的两个类属性即可,其他的不用动
	    queryset = Publish.objects.all()
    	serializer_class = PublishSerializer
# GenericAPIView源码分析
	-1 继承了APIView
    -2 有些类属性--》目前只记住两个queryset,serializer_class
    	queryset        # 要序列化的所有数据
        serializer_class  # 序列化类
        lookup_field = 'pk' # 查询单条,前端传入的参数对应值【pk】,转换器
        filter_backends  # 后续要学的,过滤
        pagination_class # 后续要学的,分页
        
   -3 有些对象方法
	   -get_queryset: 返回待序列化的数据
    		1 调用 .all 
        	2 在子类中重写,控制要序列化的数据
       -get_serializer: 返回 序列化类  以后用它
    		-本质就是---》 self.serializer_class(instance=object_list, many=True) 
             -内部调用了:self.get_serializer_class
             -后期在子类中重写get_serializer_class,返回什么序列化类,以后就以哪个序列化类做序列化
       - get_serializer_class 它是用来重写的
        def get_serializer_class(self):
            if self.request.method=='GET':
                return '序列化的类'
            else:
                return '反序列化的类'
            
      -get_object 获取单条---》根据它:lookup_field   获取

五个视图扩展类

继承GenericAPIView+5个视图扩展类+序列化类+Response

python 复制代码
# 第三层:继承GenericAPIView+5个视图扩展类+序列化类+Response
from rest_framework.mixins import CreateModelMixin, ListModelMixin, RetrieveModelMixin, UpdateModelMixin,DestroyModelMixin


class BookView(GenericAPIView, CreateModelMixin, ListModelMixin):
    # 配置两个类属性
    queryset = Book.objects.all()
    serializer_class = BookSerializer

    def get(self, request):
        return super().list(request)

    def post(self, request):
        return super().create(request)


class BookDetailView(GenericAPIView, RetrieveModelMixin, DestroyModelMixin, UpdateModelMixin):
    queryset = Book.objects.all()
    serializer_class = BookSerializer

    def put(self, request, *args, **kwargs):
        return super(BookDetailView, self).update(request, *args, **kwargs)

    def get(self, request, *args, **kwargs):
        return super().retrieve(request, *args, **kwargs)

    def delete(self, request, *args, **kwargs):
        return super().destroy(request, *args, **kwargs)

九个视图子类

这九个是视图子类,不需要额外继承GenericAPIView,只需要继承9个中其中某个,就会有某个或某几个接口,可以点击ListAPIView源码分析可知,该视图类有一个get方法,返回是一个list;该类继承的是GenericAPIView,父类有的方法,子类都可以使用,所以不再需要额外继承GenericAPIView

python 复制代码
""" 基于上面再封装成九个视图类"""
from rest_framework.generics import ListAPIView, CreateAPIView, ListCreateAPIView
from rest_framework.generics import RetrieveAPIView, UpdateAPIView, DestroyAPIView, RetrieveDestroyAPIView, \
    RetrieveUpdateAPIView, RetrieveUpdateDestroyAPIView  # 没有DestroyUpdateAPIView方法,前提是没有查询删除不了

# 查询图书所有
class BookListView(ListAPIView):
    queryset = Book.objects.all()
    serializer_class = BookSerializer

# 图书新增
class BookCreateView(CreateAPIView):
    queryset = Book.objects.all()
    serializer_class = BookSerializer

# 图书修改
class BookUpdateView(UpdateAPIView):
    queryset = Book.objects.all()
    serializer_class = BookSerializer

# 查询图书单条
class BookRetrieveView(RetrieveAPIView):
    queryset = Book.objects.all()
    serializer_class = BookSerializer

# 删除图书
class BookDestroyView(DestroyAPIView):
    queryset = Book.objects.all()
    serializer_class = BookSerializer

# 图书查询和删除功能
class BookRetrieveDestroyView(RetrieveDestroyAPIView):
    queryset = Book.objects.all()
    serializer_class = BookSerializer

# 图书查询和修改功能
class BookRetrieveUpdateView(RetrieveUpdateAPIView):
    queryset = Book.objects.all()
    serializer_class = BookSerializer

# 图书查询、修改和删除
# 查询图书单条
class BookRetrieveUpdateDestroyView(RetrieveUpdateDestroyAPIView):
    queryset = Book.objects.all()
    serializer_class = BookSerializer
	
	
""" 总结:
ListAPIView + CreateAPIView  = ListCreateAPIView    给BookView继承的
RetrieveAPIView + DestroyAPIView = RetrieveDestroyAPIView  给BookDetailView视图提供的
RetrieveAPIView + UpdateAPIView  = RetrieveDestroyAPIView  给BookDetailView视图提供的
RetrieveAPIView + UpdateAPIView + DestroyAPIView = RetrieveUpdateDestroyAPIView   给BookDetailView视图提供的

发现Destroy 和Update 方法没有整合,

"""

最终通过继承可以写成下面两个类

python 复制代码
""" 将上述的功能再整合一起"""
# 路由

urlpatterns = [
    path('books/', views.BookView.as_view()),
    path('books/<int:pk>/', views.BookView.as_view()),
]

# 视图

class BookAPIView(ListCreateAPIView):# 查询所有和新增一个
    queryset = Book.objects.all()
    serializer_class = BookSerializer

class BookDetailView(RetrieveUpdateDestroyAPIView): # 查询单条、更新和删除
    queryset = Book.objects.all()
    serializer_class = BookSerializer

基于ModelViewSet继承,编写5个类

要编写五个接口,对应写两个视图类,配置两条路由,通过观察发现连个视图类的代码几乎一模一样,我们借此引入了ModelViewSet类,继承自GenericAPIView,继承它只需要编写一个视图类

改变了路由写法--->

python 复制代码
    path('books/', views.BookAPIView.as_view({'get':'list','post':'create'})),
    path('books/<int:pk>', views.BookAPIView.as_view({'get':'retrieve','put':'update','delete':'destroy'})),

在路由里面指名两个get方法分别对应的类具体方法,当是get请求,访问这个地址,就执行视图类的list方法或retrieve方法

视图类:

python 复制代码
from rest_framework.viewsets import ModelViewSet


class BookAPIView(ModelViewSet):
    queryset = Book.objects.all()
    serializer_class = BookSerializer

点开ModelViewSet源码可知,ModelViewSet封装了ListAPIView、 CreateAPIView、RetrieveAPIView、UpdateAPIView、DestroyAPIView,直接继承ModelViewSet可以直接使用create、list、retrieve、destroy和update五种方法,但是我们查询单表和多条都是get的方法,所以需要在路由指名两个路由的get方法分别对应内置的方法( path('books/', views.BookAPIView.as_view({'get': 'list', 'post': 'create'})))

ViewSetMixin源码分析

只要是继承了ViewSetMixin,路由写法就变了

ViewSetMixin,不是视图类,支持路由映射的写法,核心原理是重写了as_view

请求来了:

原来:执行APIView的as_view内的view(request)

现在:执行ViewSetMixin的as_view内的view(request)

python 复制代码
class ViewSetMixin:  
    @classonlymethod
    def as_view(cls, actions=None, **initkwargs):
    	# actions={'get':'list','post':'create'}
        def view(request, *args, **kwargs):
            self = cls(**initkwargs) # self是BookView 视图类的对象

            if 'get' in actions and 'head' not in actions:
                actions['head'] = actions['get']
            self.action_map = actions
            for method, action in actions.items():
            # 每次循环都是method:get,actions:list
            # 去视图类对象中self反射list方法
            # handler就是BookView的对象中的list方法
                handler = getattr(self, action)
                # 反射,设置值,把method:get,设置成list方法
                # BookView类的对象,以后就是get方法也就是list方法
                setattr(self, method, handler)
            return self.dispatch(request, *args, **kwargs)
            # 根据请求方式执行跟请求方式同名的方法,get请求-----》》》get方法
ViewSetMixin的总结

以后路由写法as_view()必须传入字典,写成映射关系

什么请求方式,就会去执行视图类中什么方法,根据映射关系去执行

以后只要继承了APIView,但是路由写法变化,就要继承ViewSetMixin

以后只要继承了GenericAPIView,但是路由写法变化,就要继承ViewSetMixin

python 复制代码
视图类
# ViewSetMixin必须在APIView的前面
class UserView(ViewSetMixin, APIView):
    def login(self, reqeust):
        return Response('login')

路由
path('users/', views.UserView.as_view({'post':'login'})),
ReadOnlyModelViewSet(只读)

也是继承了GenericAPIView,但是内部封装的方法只有两个list、retrieve,只查询,查询单条和所有

路由写法也改变了

python 复制代码
	只能写两个方法,写多个会报错
    path('books/', views.BookAPIView.as_view({'get':'list'})),
    path('books/<int:pk>', views.BookAPIView.as_view({'get':'retrieve'})),

drf之路由

视图类没有继承了ViewSetMixin,路由写法跟之前一样
python 复制代码
path('books/', views.BookView.as_view())
只要视图类继承了ViewSetMixin,路由写法必须写成映射的方式
python 复制代码
path('books/', views.BookView.as_view({'get': 'list'})),

只要视图类继承了ModelViewSet,还可以这么写

python 复制代码
# 导入
from rest_framework.routers import SimpleRouter

# 实例化
router=SimpleRouter()

# 注册
router.register('books',views.BookAPIView,'books')
# 这句话就是相当于写了这两句
path('books/', views.BookAPIView.as_view({'get':'list','post':'create'})),
path('books/<int:pk>', views.BookAPIView.as_view({'get':'retrieve','put':'update','delete':'destroy'})),

# 添加到路由中
urlpatterns += router.urls

假如视图类中有个login,如何应对

python 复制代码
from rest_framework.decorators import action

class BookAPIView(ModelViewSet):
    queryset = Book.objects.all()
    serializer_class = BookSerializer
    # 手动映射
    # methods=None,请求方式
    # detail=None,只能写True或False,如果写了False就是不带pk的路径,如果写了True就是带了pk的路径
    # url_path=None,路径,会在之前的路径上,拼上这个路径,如果不写默认以函数名拼接
    # url_path='login' 写了login就会在http://127.0.0.1:8000/api/v1/books/login/,books后面加上login
    # url_name=None,别名,用作反向解析

    @action(methods=['POST','GET'], detail=False)  # 这个时候就只能POST,GET请求,别的请求不支持
    def login(self, request):
        return Response('login')
    @action(methods=['POST'], detail=False)  # 这个时候就只能POST请求,别的请求不支持
    def register(self, request):
        return Response('login')
总结

以后只要是继承了ViewSetMixin,就可以使用SimpleRouter方式写路由

python 复制代码
	#1 导入
    from rest_framework.routers import SimpleRouter,DefaultRouter
    #2 实例化 :SimpleRouter,DefaultRouter
    router = SimpleRouter()
    或:认为他们一样即可---》DefaultRouter多一条路径
    router = DefaultRouter()
    #3 注册路径
    router.register('books', views.BookView, 'books')
    #4 加入到路由中:
    # 方式一:(用这个)
    urlpatterns += router.urls
    # 方式二:
    urlpatterns = [
        path('', include(router.urls)),
    ]
    # 5 list,create,retrieve,destroy,update--->自动映射--》SimpleRouter
    # 6 视图类中自己的方法,再做映射--action装饰器
    @action(methods=['POST'],detail=False,)
    def login(self,request):
        return Response('login')

认证组件

登录进系统后,再访问接口信息,需要携带登录信息,如果没携带就不允许访问,这个控制就是认证

之前我们学过cookie(浏览器自带的)和Session(后端存储的键值对)

写个登录

models.py

python 复制代码
# 用户表用来做登录
class User(models.Model):
    username = models.CharField(max_length=64)
    password = models.CharField(max_length=64)


class UserToken(models.Model):
    user = models.OneToOneField(to='User', on_delete=models.CASCADE)
    token = models.CharField(max_length=64)

views.py

python 复制代码
from .models import User, UserToken
from rest_framework.viewsets import ViewSet
import uuid


class UserView(ViewSet):
    @action(methods=['POST'], detail=False)
    def login(self, request):
        username = request.data.get('username')
        password = request.data.get('password')
        user = User.objects.filter(username=username, password=password).first()
        if user:
            # 生成随机字符串,放到UserToken表中,把随机字符串返回给前端
            token = str(uuid.uuid4())
            # 如果之前UserToken中有数据,就要更新,没有就有新增
            UserToken.objects.update_or_create(defaults={'token': token}, user_id=user.pk)
            return Response({'code': 100, 'msg': 'ok', 'token': token})
        else:
            return Response({'code': 101, 'msg': 'no'})

app01>>>urls

python 复制代码
# 导入
from rest_framework.routers import SimpleRouter

# 实例化
router=SimpleRouter()

# 注册
# router.register('books',views.BookAPIView,'books')
router.register('users',views.UserView,'users')
urlpatterns = []
# 添加到路由中
urlpatterns += router.urls

每当我提交一次POST请求,token就会刷新,UserToken表里的数据也会刷新

认证组件步骤

1.写一个认证类,继承BaseAuthentication

2.在类中重写 authenticate,在方法中完成认证,如果通过,返回两个值,如果失败,抛异常

python 复制代码
        def authenticate(self, request):
            # 完成对用户的校验
            # 当次请求request
            token = request.query_params.get('token')
            # 表中校验
            user_token = UserToken.objects.filter(token=token).first()
            # 当前登录用户
            if user_token:
                user = user_token.user

                # 校验过后,返回两个值
                return user, user_token
            else:
                raise AuthenticationFailed("token不合法")

3.是用认证类:需要放在登录后才能访问的视图类上

python 复制代码
class BookView(ViewSet,ListCreateAPIView):
    queryset = Book.objects.all()
    serializer_class = BookSerializer


class BookDetailView(ViewSet,RetrieveUpdateDestroyAPIView):
    authentication_classes = [LoginAuth]
    queryset = Book.objects.all()
    serializer_class = BookSerializer

4.配置文件中配置

在drf的配置文件中找需要的参数,将它拷贝到项目的配置文件中

settings.py

python 复制代码
将我们自己的认证模块导入进来,全局配置登录认证
REST_FRAMEWORK = {
	    'DEFAULT_AUTHENTICATION_CLASSES': [
        'app01.auth.LoginAuth',
    ],
}

但是这样配置以后就会出问题,所有的接口都需要走登录认证,我们只需要在需要开放的接口视图类中加上:

python 复制代码
authentication_classes = []  # 括号里不需要加任何参数
相关推荐
凡人的AI工具箱2 小时前
每天40分玩转Django:Django表单集
开发语言·数据库·后端·python·缓存·django
工业互联网专业7 小时前
Python毕业设计选题:基于python的酒店推荐系统_django+hadoop
hadoop·python·django·vue·毕业设计·源码·课程设计
任小永的博客7 小时前
VUE3+django接口自动化部署平台部署说明文档(使用说明,需要私信)
后端·python·django
凡人的AI工具箱7 小时前
每天40分玩转Django:Django类视图
数据库·人工智能·后端·python·django·sqlite
余生H7 小时前
前端Python应用指南(三)Django vs Flask:哪种框架适合构建你的下一个Web应用?
前端·python·django
知识的宝藏7 小时前
Django models中的增删改查与MySQL SQL的对应关系
sql·mysql·django·django models
凡人的AI工具箱7 小时前
每天40分玩转Django:实操图片分享社区
数据库·人工智能·后端·python·django
HEX9CF8 小时前
【Django】测试带有 CSRF 验证的 POST 表单 API 报错:Forbidden (CSRF cookie not set.)
python·django·csrf
凡人的AI工具箱9 小时前
每天40分玩转Django:实操多语言博客
人工智能·后端·python·django·sqlite
Cachel wood9 小时前
Django REST framework (DRF)中的api_view和APIView权限控制
javascript·vue.js·后端·python·ui·django·前端框架