摘要:
class APIResponse(Response): def __init__(self,code=100,msg='成功',data=None,status=None,headers=None,**kwargs): dic = {'code': code, 'msg': msg} if dat 阅读全文
posted @ 2022-04-06 18:13
咖喱给给啊
阅读(49)
评论(0)
推荐(0)
摘要:
开启事务: start transaction; 开启事务后需要手动提交,没有提交的话数据库中表的数据不会发生变化, 虽然表里的数据没有变化,但是其实数据已经写入了,只是没有提交, BEGIN;UPDATE `test` SET `name` = "1"; 开始事务执行了这条sql,执行完毕后 数据 阅读全文
posted @ 2022-04-06 18:12
咖喱给给啊
阅读(35)
评论(0)
推荐(0)
摘要:
#统一接口返回 # 自定义异常方法,替换掉全局 # 写一个方法 # 自定义异常处理的方法 from rest_framework.views import exception_handler from rest_framework.response import Response from rest 阅读全文
posted @ 2022-04-06 03:14
咖喱给给啊
阅读(38)
评论(0)
推荐(0)
摘要:
from rest_framework.generics import ListAPIView from rest_framework.filters import OrderingFilter from app01.models import Book from app01.ser import 阅读全文
posted @ 2022-04-06 03:10
咖喱给给啊
阅读(9)
评论(0)
推荐(0)
摘要:
#1 安装:pip3 install #2 注册,在app中注册 #3 全局配,或者局部配 'DEFAULT_FILTER_BACKENDS': ('django_filters.rest_framework.DjangoFilterBackend',) #4 视图类 class BookView( 阅读全文
posted @ 2022-04-06 03:09
咖喱给给啊
阅读(13)
评论(0)
推荐(0)
摘要:
# 全局使用 限制未登录用户1分钟访问5次 REST_FRAMEWORK = { 'DEFAULT_THROTTLE_CLASSES': ( 'rest_framework.throttling.AnonRateThrottle', ), 'DEFAULT_THROTTLE_RATES': { 'a 阅读全文
posted @ 2022-04-06 03:08
咖喱给给啊
阅读(38)
评论(0)
推荐(0)
摘要:
# 需求:未登录用户1分钟访问5次,登录用户一分钟访问10次 全局:在setting中 'DEFAULT_THROTTLE_CLASSES': ( 'rest_framework.throttling.AnonRateThrottle', 'rest_framework.throttling.Use 阅读全文
posted @ 2022-04-06 03:08
咖喱给给啊
阅读(37)
评论(0)
推荐(0)
摘要:
# 演示一下内置权限的使用:IsAdminUser,控制是否对网站后台有权限的人 # 1 创建超级管理员 # 2 写一个测试视图类 from rest_framework.permissions import IsAdminUser from rest_framework.authenticatio 阅读全文
posted @ 2022-04-06 03:03
咖喱给给啊
阅读(34)
评论(0)
推荐(0)
摘要:
APIView >dispatch >initial >self.check_permissions(request)(APIView的对象方法) def check_permissions(self, request): # 遍历权限对象列表得到一个个权限对象(权限器),进行权限认证 for pe 阅读全文
posted @ 2022-04-06 02:58
咖喱给给啊
阅读(121)
评论(0)
推荐(0)
摘要:
# 写一个认证类 app_auth.py from rest_framework.authentication import BaseAuthentication from rest_framework.exceptions import AuthenticationFailed from app0 阅读全文
posted @ 2022-04-06 02:56
咖喱给给啊
阅读(25)
评论(0)
推荐(0)
摘要:
# 认证的实现 1 写一个类,继承BaseAuthentication,重写authenticate,认证的逻辑写在里面,认证通过,返回两个值,一个值最终给了Requet对象的user,认证失败,抛异常:APIException或者AuthenticationFailed 2 全局使用,局部使用 ` 阅读全文
posted @ 2022-04-06 02:54
咖喱给给啊
阅读(28)
评论(0)
推荐(0)
摘要:
# action干什么用?为了给继承自ModelViewSet的视图类中定义的函数也添加路由 # 使用 class BookViewSet(ModelViewSet): queryset =Book.objects.all() serializer_class = BookSerializer # 阅读全文
posted @ 2022-04-06 02:52
咖喱给给啊
阅读(99)
评论(0)
推荐(0)
摘要:
# 1 在urls.py中配置 path('books4/', views.Book4View.as_view()), re_path('books4/(?P<pk>\d+)', views.Book4DetailView.as_view()), # 2 一旦视图类,继承了ViewSetMixin, 阅读全文
posted @ 2022-04-06 02:51
咖喱给给啊
阅读(31)
评论(0)
推荐(0)
摘要:
from rest_framework.viewsets import ViewSetMixin class Book6View(ViewSetMixin,APIView): #一定要放在APIVIew前 def get_all_book(self,request): print("xxxx") b 阅读全文
posted @ 2022-04-06 02:47
咖喱给给啊
阅读(14)
评论(0)
推荐(0)
摘要:
from rest_framework.viewsets import ModelViewSet class Book5View(ModelViewSet): #5个接口都有,但是路由有点问题 queryset = Book.objects serializer_class = BookSerial 阅读全文
posted @ 2022-04-06 02:46
咖喱给给啊
阅读(77)
评论(0)
推荐(0)
摘要:
from rest_framework.mixins import ListModelMixin,CreateModelMixin,UpdateModelMixin,DestroyModelMixin,RetrieveModelMixin # views.py class Book3View(Gen 阅读全文
posted @ 2022-04-06 02:45
咖喱给给啊
阅读(28)
评论(0)
推荐(1)
摘要:
# views.py class Book2View(GenericAPIView): #queryset要传queryset对象,查询了所有的图书 # serializer_class使用哪个序列化类来序列化这堆数据 queryset=Book.objects # queryset=Book.ob 阅读全文
posted @ 2022-04-06 02:44
咖喱给给啊
阅读(18)
评论(0)
推荐(0)
摘要:
#### views.py from rest_framework.generics import GenericAPIView from app01.models import Book from app01.ser import BookSerializer # 基于APIView写的 clas 阅读全文
posted @ 2022-04-06 02:42
咖喱给给啊
阅读(29)
评论(0)
推荐(0)
摘要:
#from rest_framework.response import Response def __init__(self, data=None, status=None, template_name=None, headers=None, exception=False, content_ty 阅读全文
posted @ 2022-04-06 02:40
咖喱给给啊
阅读(21)
评论(0)
推荐(0)
摘要:
# 请求对象 # from rest_framework.request import Request def __init__(self, request, parsers=None, authenticators=None, negotiator=None, parser_context=Non 阅读全文
posted @ 2022-04-06 02:38
咖喱给给啊
阅读(44)
评论(0)
推荐(0)
浙公网安备 33010602011771号