首页 > 其他分享 >drf-06

drf-06

时间:2022-10-08 19:44:43浏览次数:43  
标签:06 self args request kwargs return def drf

路由层

自动生成路由

 1.路由类导入

from rest_framework.routers import SimpleRouter, DefaultRouter

 2.实例化

router = SimpleRouter()

 3.注册

router.register('login', BookView, 'login')

 4.将自动生成的路由添加到urlpatterns中(两种方式结果一样)

方式一:urlpatterns += router.urls
方式二:path('login/', include(router.urls))

 5.自动生成的路由

5.1 使用路由类SimpleRouter 
    router = SimpleRouter()
5.2 使用路由类DefaultRouter
    router = DefaultRouter()

 注:继承ViewSetMixin的类及其子类的类可以使用自动生成路由

视图层

action装饰器的使用

 1.装饰器导入:from rest_framework.decorators import action
 2.action装饰器的参数
  2.1 methods:请求类型 可多个
  2.2 detail:控制生成的网址
   默认False:注册路由 router.register('user', BookView, 'user')
user/login
   True: user/pk/login
  2.3 url_path:控制生成的路由user/后面的路径 默认是方法名
  2.4 url_name:起别名 用于反向解析

class UserView(ModelViewSet):
    queryset = Book.objects.all()
    serializer_class = BookSerializer
    @action(methods=['GET', ], detail=False)
    def login(self, request):
        return Response('登录成功')

编写登录接口

路由层

1.导入路由类:SimpleRouter
2.实例化:router = SimpleRouter()
3.注册
4.将自动生成的路由添加至urlpatterns 
from django.urls import path, include(路由分发)
from rest_framework.routers import SimpleRouter
from app01.views import UserView

router = SimpleRouter()
router.register('user', UserView, 'user')
urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include(router.urls))
]

视图层

1.继承ViewSet==ViewSetMixin, views.APIView 类中的方法可以写任意方法名
2.
from rest_framework.viewsets import ViewSet
from rest_framework.decorators import action
from rest_framework.response import Response
from app01.models import User, UserToken
from uuid import uuid4

class UserView(ViewSet):
    @action(methods=['POST', ], detail=False)
    def login(self, request):
        name = request.data.get('name')
        password = request.data.get('password')
        user = User.objects.filter(name=name, password=password).first()   
        if user:
            token = str(uuid4())
            UserToken.objects.update_or_create(defaults={'token': token}, user=user)
            return Response({'code': 100, 'mag': '登录成功'})
        else:
            return Response({'code': 101, 'msg': '用户名或密码错误'})

认证

1.写认证类继承BaseAuthentication
2.重写父类BaseAuthentication中的authenticate方法
3.获取网址中携带的随机字符串 # 类似于cookie与session操作
4.获取数据库数据判断是否一致 存在可以访问 返回 user_token.user(后续可以使用), token
5.否则直接抛异常AuthenticationFailed
6.全局配置认证类:在配置文件中配置

REST_FRAMEWORK={'DEFAULT_AUTHENTICATION_CLASSES':['app01.auth.LoginAuth',]}
  局部配置:类中authentication_classes = [auth.LoginAuth,]
  局部禁用:类中authentication_classes = []
from rest_framework.authentication import BaseAuthentication
from app01.models import UserToken
from rest_framework.exceptions import AuthenticationFailed

class LoginAuth(BaseAuthentication):
    def authenticate(self, request):
        token = request.GET.get('token')  # 获取网址中携带的随机字符串
        user_token = UserToken.objects.filter(token=token).first()
        if not user_token:
            raise AuthenticationFailed('你没有登录')
        return user_token.user, token

作业

 1.1 登录接口,图书5个接口,出版社5个接口(关联的关系),,使用9个视图子类,5个视图扩展类写

5个视图扩展类写图书与出版社5个接口
路由层:
urlpatterns = [
    path('books/', BookView.as_view()),
    path('books/<int:pk>', BookDetailView.as_view()),
    path('publish/', PublishView.as_view()),
    path('publish/<int:pk>', PublishDetailView.as_view())
]
序列化类:
from rest_framework import serializers
from app01.models import Book, Publish

class BookSerializer(serializers.ModelSerializer):
    class Meta:
        model = Book
        fields = '__all__'

    publish = serializers.SerializerMethodField()

    def get_publish(self, obj):
        return {'name': obj.publish.name, 'address': obj.publish.address}

class PublishSerializer(serializers.ModelSerializer):
    class Meta:
        model = Publish
        fields = '__all__'
 视图层:
图书查所有、增加
class BookView(ListModelMixin, CreateModelMixin, GenericAPIView):
    queryset = models.Book.objects.all()
    serializer_class = BookSerializer

    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 BookDetailView(RetrieveModelMixin, UpdateModelMixin, DestroyModelMixin, GenericAPIView):

    queryset = models.Book.objects.all()
    serializer_class = BookSerializer

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

    def put(self, request, pk):
        return self.update(request)

    def delete(self, request, *args, **kwargs):
        return self.destroy(request, *args, **kwargs)
出版社查所有、增加
class PublishView(ListModelMixin, CreateModelMixin, GenericAPIView):

    queryset = models.Publish.objects.all()
    serializer_class = PublishSerializer

    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 PublishDetailView(RetrieveModelMixin, UpdateModelMixin, DestroyModelMixin, GenericAPIView):

    queryset = models.Publish.objects.all()
    serializer_class = PublishSerializer

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

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

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

 2 图书的必须登录后才能方法,出版社的不登录就能访问

1.认证类
from rest_framework.authentication import BaseAuthentication
from app01.models import UserToken
from rest_framework.exceptions import AuthenticationFailed

class LoginAuth(BaseAuthentication):
    def authenticate(self, request):
        token = request.GET.get('token')  # 获取网址中携带的随机字符串
        user_token = UserToken.objects.filter(token=token).first()
        if not user_token:
            raise AuthenticationFailed('你没有登录')
        return user_token.user, token
 2.全局配置:所有视图类都需登录才可以访问
  在配置文件中配置:
    REST_FRAMEWORK = {'DEFAULT_AUTHENTICATION_CLASSES': ['app01.auth.LoginAuth', ]}
 3.图书的必须登录后才能方法
class BookView(ListModelMixin, CreateModelMixin, GenericAPIView):
    queryset = models.Book.objects.all()
    serializer_class = BookSerializer

    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 BookDetailView(RetrieveModelMixin, UpdateModelMixin, DestroyModelMixin, GenericAPIView):

    queryset = models.Book.objects.all()
    serializer_class = BookSerializer

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

    def put(self, request, pk):
        return self.update(request)

    def delete(self, request, *args, **kwargs):
        return self.destroy(request, *args, **kwargs)
   4.出版社的不登录就能访问;局部禁用 authentication_classes = []
class PublishView(ListModelMixin, CreateModelMixin, GenericAPIView):
    authentication_classes = []
    queryset = models.Publish.objects.all()
    serializer_class = PublishSerializer

    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 PublishDetailView(RetrieveModelMixin, UpdateModelMixin, DestroyModelMixin, GenericAPIView):

    authentication_classes = []

    queryset = models.Publish.objects.all()
    serializer_class = PublishSerializer

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

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

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

 3.级联删除中的参数(on_delete中的参数中的值)

1.on_delete=models.None:删除关联表中的数据时,当前表与其关联的field的行为
2.on_delete=models.CASCADE:级联删除,删除关联关系,与之关联也删除
3.on-delete=models.DO_NOTHING:删除管来呢数据,什么也不做
4.on_delete=models.PRPTECT:保护模式,删除关联数据,引发错误protectedError
5.on_delete=models.SET_NULL:置空模式,删除的时候,外键字段被设置为空,前提就是blank=True, null=True,定义该字段的时候,允许为空。当Manufacturer对象删除时,它对应的Car对象的manufacturer字段会置空,前提是null=True
6.on_delete=models.SET_DEFAULT:删除关联数据,与之关联的值设置为默认值(前提FK字段需要设置默认值,一对一同理)置默认值,删除的时候,外键字段设置为默认值,所以定义外键的时候注意加上一个默认值。
7.on_delete=models.SET:删除关联数据,自定义一个值,该值当然只能是对应的实体了
  7.1 与之关联的值设置为指定值,设置:models.SET(值)
  7.2 与之关联的值设置为可执行对象的返回值,设置:models.SET(可执行对象)

标签:06,self,args,request,kwargs,return,def,drf
From: https://www.cnblogs.com/040714zq/p/16770002.html

相关文章

  • drf学习笔记
    今日内容概要路由登录接口编写认证今日内容详细路由对于视图集ViewSet,我们除了可以自己手动指明请求方式与动作action之间的对应关系,还可以使用Routers来帮助......
  • 【致12306】虽然我知道你很烂,但我没想到你会乱成这样,还不如没有!很失望!
    现在是北京时间2013年12月24日13点30分,我想买一张从海拉尔到哈尔滨的票,结果发现一天之隔 票价贵了好多!!!怎么回事,铁路部有说要涨价的吗?还涨的这么的离谱!!!下面是从网上截......
  • drf-6
    前5天内容小回顾一、drf入门亲后端开发模式    分离:drf方便我们快速写出接口    混合:bbs、模板语法API接口:前后端交互的媒介接口测试工具:postman(不止这一......
  • drf之路由类与认证类
    一、自动生成路由对于我们的视图类只要继承了ViewSetMixin及其子类那么路由的写法就需要改变path('books/',views.BookView.as_view({'get':'list'}))这个时候有人写......
  • SPI(硬件)---OLED&SSD1306的使用
    先说结论:proteus貌似不是很支持硬件I2C和硬件SPI。。两次试验都没成功但基本的原理还是记录一下 工具软件:Keil、CUBEMX、Proteus 硬件:单片机---STM32F103C6......
  • drf_1~5
    前后端开发模式1.前后端不分离----全栈涉及模板语法2.前后端分离2.1前端测试工具:模仿发送http请求--postman2.2api接口:连接前后端的媒介restful规范1.使用https......
  • 代码随想录训练营|Day 18|513, 112, 113, 105, 106
    513.FindBottomLeftTreeValueGiventhe root ofabinarytree,returntheleftmostvalueinthelastrowofthetree.Example1:Input:root=[2,1,3]Ou......
  • leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal 从中序
    一、题目大意给定两个整数数组inorder和postorder,其中inorder是二叉树的中序遍历,postorder是同一棵树的后序遍历,请你构造并返回这颗二叉树。示例1:输入:ino......
  • Solution -「SDOI 2017」「洛谷 P3706」硬币游戏
    \(\mathscr{Description}\)  Link.  给定\(n\)个长度为\(m\)且两两不同的字符串\(S_{1..n}\),字符集\(|\Sigma|=2\).各位置独立在\(\Sigma\)中均匀随机,......
  • 基础06:聚合函数
    一、聚合函数介绍什么是聚合函数聚合函数作用于一组数据,并对一组数据返回一个值。聚合函数类型AVG();SUM();MAX();MIN();COUNT();1.1AVG和SUM函数可......