首页 > 其他分享 >Day 89 Django_视图封装 & 路由 & 版本控制组件

Day 89 Django_视图封装 & 路由 & 版本控制组件

时间:2023-02-10 20:12:46浏览次数:41  
标签:版本控制 models self py request 视图 Django import class

视图封装

model.py 文件

from django.db import models


# Create your models here.

class Book(models.Model):
    title = models.CharField(max_length=32, verbose_name='图书名称')
    CHOICES = ((1, 'Java'), (2, 'MySQL'), (3, 'Python'),)
    category = models.IntegerField(choices=CHOICES, verbose_name='图书分类')
    pub_time = models.DateField(verbose_name='出版社日期')

    publisher = models.ForeignKey(to='Publisher',on_delete=callable,db_constraint=False)
    author = models.ManyToManyField(to='Author')

    def __str__(self):
        return self.title

    class Meta:
        verbose_name_plural = '01_图书表'
        db_table = verbose_name_plural


class Publisher(models.Model):
    title = models.CharField(max_length=32,verbose_name='出版社信息')

    def __str__(self):
        return self.title

    class Meta:
        verbose_name_plural = '02_出版社表'
        db_table = verbose_name_plural


class Author(models.Model):
    Author_name = models.CharField(max_length=32, verbose_name='作者')

    def __str__(self):
        return self.Author_name

    class Meta:
        verbose_name_plural = '03_作者表'
        db_table = verbose_name_plural
model.py

urls.py 文件

from django.urls import path, include
from .views import BookView, BookEditView, BookTwoEditView

urlpatterns = [
    path('list',BookView.as_view()),
    path('EditBook/<int:id>',BookEditView.as_view())
    # path('list', BookTwoEditView.as_view({"get": "list", "post": "create"})),
    # path('EditBook/<int:id>', BookTwoEditView.as_view({"get": "retrieve", "put": "edit_View", "delete": "eidt_delete"}))
]
urls.py

serializers.py 文件

from rest_framework import serializers
from .models import Book

class BookSerializer(serializers.ModelSerializer):
    category_display = serializers.SerializerMethodField(read_only=True)
    publisher_info = serializers.SerializerMethodField(read_only=True)
    authors = serializers.SerializerMethodField(read_only=True)

    def get_category_display(self, obj):
        return obj.get_category_display()

    # 通过自定义 get_publisher 与 get_author 来获取我们想要的指定字段
    def get_publisher_info(self, obj):
        # obj 是我们序列化的每个 Book 对象
        publisher_obj = obj.publisher
        return {"id": publisher_obj.id, "title": publisher_obj.title}

    def get_authors(self, obj):
        author_obj = obj.author.all()
        return [{'id': author.id, 'Author_name': author.Author_name} for author in author_obj]

    class Meta:
        model = Book
        # fields = ['id','title','pub_time']  # 拿到指定信息
        fields = '__all__'  # 拿到所有信息,但存在关联字段 id 信息,不利于阅读,需要单独处理
        # depth = 1   # 获取关联表中的字段,数字代表 深度,但这样回取出所有数据

        # extra_kwargs 添加字段的属性信息
        extra_kwargs = {"category": {"write_only": True}, "publisher": {"write_only": True},
                        "author": {"write_only": True}}
serializers.py

views.py 文件

from .models import Book, Publisher
from .serializers import BookSerializer
from rest_framework.views import APIView
from rest_framework.response import Response

class GenericAPIView(APIView):
    query_set = None
    serializers_class = None

    def get_queryset(self):
        return self.query_set

    def get_serializer(self, *args, **kwargs):
        return self.serializers_class(*args, **kwargs)


class GetListModelMixin(object):
    def list(self, request):
        queryset = self.get_queryset()
        ret = self.get_serializer(queryset, many=True)
        return Response(ret.data)


class PostCreateModeMixin(object):
    def create(self, request):
        serializers = self.get_serializer(data=request.data)
        if serializers.is_valid():
            serializers.save()
            return Response(serializers.data)
        else:
            return Response(serializers.errors)


class RetrieveModeMixin(object):
    def retrieve(self, request, id):
        book_obj = self.get_queryset().filter(id=id).first()
        ret = self.get_serializer(book_obj)
        return Response(ret.data)


class EditViewModeMixin(object):
    def edit_View(self, request, id):
        book_obj = self.get_queryset().filter(id=id).first()
        ret = self.get_serializer(book_obj, data=request.data, partial=True)  # partial 是否允许部分更新
        if ret.is_valid():
            ret.save()
            return Response(ret.data)
        else:
            return Response(ret.errors)


class EditDeleteModeMixin(object):
    def eidt_delete(self, request, id):
        book_obj = self.get_queryset().filter(id=id).first()
        book_obj.delete()
        return Response('已删除')


class BookViewListClass(GenericAPIView, GetListModelMixin, PostCreateModeMixin):
    pass


class BookView(BookViewListClass):  # 继承 GenericAPIView 这个通用类
    query_set = Book.objects.all()
    serializers_class = BookSerializer

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

    # Post 请求
    def post(self, request):
        return self.create(request)


class BookEditListClass(GenericAPIView, RetrieveModeMixin, EditViewModeMixin, EditDeleteModeMixin):
    pass


class BookEditView(BookEditListClass):
    query_set = Book.objects.all()
    serializers_class = BookSerializer

    def get(self, request, id):
        return self.retrieve(request, id)

    def put(self, request, id):
        return self.edit_View(request, id)

    def delete(self, request, id):
        return self.eidt_delete(request, id)
views.py

 

使用路由传参实现用一个视图完成该功能

urls.py 文件,实现路由传参 views.py 中类必须继承 ViewSetMixin

from django.urls import path, include
from .views import BookView, BookEditView, BookTwoEditView

urlpatterns = [
    # path('list',BookView.as_view()),
    # path('EditBook/<int:id>',BookEditView.as_view())
    path('list', BookTwoEditView.as_view({"get": "list", "post": "create"})),
    path('EditBook/<int:id>', BookTwoEditView.as_view({"get": "retrieve", "put": "edit_View", "delete": "eidt_delete"}))
]
urls.py

views.py 文件,新建类继承 ViewSetMixin

from rest_framework.viewsets import ViewSetMixin
# 框架提供的类 所在包位置
from rest_framework.viewsets import ModelViewSet # 这个是框架提供的 ManyViewClass 类
from rest_framework import views
from rest_framework import generics
from rest_framework import mixins
from rest_framework import viewsets


class ManyViewClass(ViewSetMixin, GenericAPIView, GetListModelMixin, PostCreateModeMixin, RetrieveModeMixin,
                    EditViewModeMixin, EditDeleteModeMixin):
    pass


class BookTwoEditView(ManyViewClass):
    query_set = Book.objects.all()
    serializers_class = BookSerializer
views.py

 

DRF 路由组件

from rest_framework.routers import DefaultRouter

 

版本控制组件介绍

settings.py 文件,新增 REST_FRAMEWORK 参数,使用 rest_framework.versioning.QueryParameterVersioning

REST_FRAMEWORK = {
    # "DEFAULT_VERSIONING_CLASS": "utils.version.MyVersion",
    "DEFAULT_VERSIONING_CLASS": "rest_framework.versioning.QueryParameterVersioning",
    "DEFAULT_VERSION": "v1",
    "ALLOWED_VERSIONS": "v1, v2",
    "VERSION_PARAM": "ver"
}
settings.py

urls.py 文件

from django.urls import path,include
from .views import DomeView

urlpatterns = [
    path(r'',DomeView.as_view()),
]
urls.py

views.py 文件

from rest_framework.views import APIView
from rest_framework.response import Response


class DomeView(APIView):
    def get(self,request):
        print(request.version)
        print(request.versioning_scheme)
        if request.version == 'v1':
            return Response('V1 版本')
        if request.version == 'v2':
            return Response('V2 版本')
        return Response('测试版本')
views.py

 

认证组件

认证组件的方法都在:import rest_framework.authentication

models.py

from django.db import models

# Create your models here.

from django.db import models
class User(models.Model):
    username = models.CharField(max_length=32)
    password = models.CharField(max_length=32)
    token = models.UUIDField()
models.py

urls.py

from django.urls import path,include
from .views import authDome,LoginDome,TestDome

urlpatterns = [
    path(r'',authDome.as_view()),
    path(r'login',LoginDome.as_view()),
    path(r'test',TestDome.as_view()),
]
urls.py

views.py

import uuid
# Create your views here.

from rest_framework.views import APIView
from rest_framework.response import Response
from utils.auth import MyAuth

from .models import User


class authDome(APIView):
    def get(self,request):
        return Response('认证视图')

class LoginDome(APIView):
    def post(self,request):
        username = request.data.get('username')
        password = request.data.get('password')
        token = uuid.uuid4()
        User.objects.create(username=username,password=password,token=token)
        return Response('创建成功')

class TestDome(APIView):
    # 局部视图配置 在需要做验证的类中配置
    authentication_class = [MyAuth,]
    def get(self,request):
        print(request.user)
        print(request.auth)
        return Response('认证测试')
views.py

auth.py 认证文件

from rest_framework.authentication import BaseAuthentication
from rest_framework.exceptions import AuthenticationFailed
from authDome.models import User


class MyAuth(BaseAuthentication):
    def authenticate(self,request):
        token = request.query_params.get("token","")
        if not token:
            raise AuthenticationFailed("没有携带 token")
        user_obj =  User.objects.filter(token=token).first()
        if not user_obj:
            raise AuthenticationFailed("携带的 token 不合法")
        return (user_obj,token)
auth.py

 

权限组件

权限组件的方法都在:import rest_framework.permissions

新建 permission.py 文件

from rest_framework.permissions import BasePermission
class MyPermission(BasePermission):
    message = '没有权限'
    def has_permission(self, request, view):
        # 判断用户是否有权限
        user_obj = request.user
        if user_obj.type == 3:
            return False
        else:
            return True
permission.py

models.py 文件

from django.db import models


class User(models.Model):
    username = models.CharField(max_length=32)
    password = models.CharField(max_length=32)
    token = models.UUIDField()
    type = models.IntegerField(choices=((1, "vip"), (2, "VVIP"), (3, "普通用户"),), default=3)
models.py

views.py 文件

from django.shortcuts import render
import uuid
# Create your views here.

from rest_framework.views import APIView
from rest_framework.response import Response
from utils.auth import MyAuth
from utils.permission import MyPermission
from .models import User


class authDome(APIView):
    def get(self,request):
        return Response('认证视图')

class LoginDome(APIView):
    def post(self,request):
        username = request.data.get('username')
        password = request.data.get('password')
        token = uuid.uuid4()
        User.objects.create(username=username,password=password,token=token)
        return Response('创建成功')

class TestDome(APIView):
    # 局部视图配置 在需要做验证的类中配置
    authentication_classes = [MyAuth, ]     
    permission_classes = [MyPermission, ]   # 添加权限类
    def get(self,request):
        print(request.user)
        print(request.auth)
        return Response('认证测试')
views.py

 

频率接口

 

标签:版本控制,models,self,py,request,视图,Django,import,class
From: https://www.cnblogs.com/ysging/p/13682240.html

相关文章

  • Django的生命周期
    目录Django的请求生命周期Django视图层视图函数的编写视图函数执行过程Django模板层模板层与视图层交互模板语法变量过滤器标签模板继承Django的请求生命周期所谓Django......
  • MySQL视图、存储过程、函数、触发器、定时任务、流程控制总结
    视图的增删改查视图相当于一张只能读的表,不可以修改。当组成视图的表发生数据变化的时候,视图会相对应的进行改变。存储过程的练习创建存储过程:create[ifnotexi......
  • Django 含有外键模型新增数据以及序列化
    https://www.cnblogs.com/SunshineKimi/p/14042914.htmlDjango原生实现外键classAppleModel(models.Model):id=models.AutoField(primary_key=True)app_name......
  • Kubernetes:分享一个可以展示资源视图的 K8s开源 Web/桌面 客户端工具 Octant
    写在前面博文内容主要为Octant介绍以及桌面/Web端的安装教程涉及Linux/Windows的安装。理解不足小伙伴帮忙指正其实当你什么都经历了,会发现,人生无论你怎么精心......
  • Django-ORM
    ORM由来让我们从O/R开始。字母O起源于"对象"(Object),而R则来自于"关系"(Relational)。几乎所有的软件开发过程中都会涉及到对象和关系数据库。在用户层面和业务逻辑层面,......
  • docker搭建django+mysql
    1.本机项目迁移到linux系统中2.编写dockerfile内容如下 WOEKDIR 在容器中创建该目录COPY 复制本机当前目录下的requirements.txt文件 到容器drfproject......
  • Django生命请求周期
    Django的请求生命周期所谓Django的请求生命周期,直白的来说就是当请求来的时候和请求走的阶段中,Django的执行轨迹。一个完整的Django生命周期:用户从客户端发出一条请求以......
  • windows搭建django开发环境
    windows如何搭建django开发环境pycharm搭建django运行环境虚拟环境搭建前置条件,下载安装python如3.11.1下载安装pycharm社区版下载安装git在gitee上新建用户,搭建......
  • git取消版本控制
    参考:https://blog.csdn.net/lzx5290/article/details/127665687?spm=1001.2014.3001.5506执行命令如下:MicrosoftWindows[版本10.0.19041.208](c)2020MicrosoftCo......
  • Django框架之drf:8、断点调试,权限、认证、频率组件源码分析,基于APIView编写分页,全局异
    目录Django框架之drf一、断点调式使用1、断点调试二、权限组件源码分析三、认证组件源码分析四、频率组件源码分析1、自定义频率类2、SimpleRateThrottle五、基于APIView编......