随笔分类 -  drf框架

drf从入门到入土
摘要:视图函数内书写如下代码: def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.request_action = None def initialize_request(self, request, * 阅读全文
posted @ 2024-01-04 21:11 wellplayed 阅读(10) 评论(0) 推荐(0) 编辑
摘要:自定义表签发token 不通过auth_user表,而是通过自定义表实现签发token 代码如下: 表模型: class UserInfo(models.Model): username = models.CharField(max_length=32) password = models.Char 阅读全文
posted @ 2024-01-04 16:35 wellplayed 阅读(26) 评论(0) 推荐(0) 编辑
摘要:restful规范(10条) 1 数据的安全保障:url链接一般都采用https协议进行传输 -https是:http+ssl 安全的超文本传输协议 2 接口特征表现:在API地址中带接口标识,咱们一般放在地址栏中(放在域名中) https://api.baidu.com https://www.b 阅读全文
posted @ 2024-01-04 09:23 wellplayed 阅读(13) 评论(0) 推荐(0) 编辑
摘要:需求如下: 用户输入:用户名或手机号或邮箱 +密码都能登录,并签发token 代码书写: model层配置(需要扩写auth_user表): from django.contrib.auth.models import AbstractUser class User(AbstractUser): m 阅读全文
posted @ 2024-01-03 21:12 wellplayed 阅读(100) 评论(0) 推荐(0) 编辑
摘要:jwt的使用步骤 第一步:安装模块 pip install djangorestframework-simplejwt 第二步:注册app # settings文件配置 INSTALLED_APPS = [ ... 'rest_framework_simplejwt', ... ] 第三步:sett 阅读全文
posted @ 2024-01-03 20:35 wellplayed 阅读(16) 评论(1) 推荐(1) 编辑
摘要:自定义全局异常处理 drf异常处理交给exception_handler处理了,但是没处理非drf的异常 'EXCEPTION_HANDLER': 'rest_framework.views.exception_handler' 我们可以重写一个exception_handler方法,处理drf异常 阅读全文
posted @ 2023-12-28 17:00 wellplayed 阅读(85) 评论(0) 推荐(0) 编辑
摘要:分页组件的使用 有三种分页方式 需要新建一个py文件,以pagination.py为例 方式一:基本分页 第一步:导入分页类 from rest_framework.pagination import PageNumberPagination 第二步:书写分页类,继承 PageNumberPagin 阅读全文
posted @ 2023-12-28 16:37 wellplayed 阅读(33) 评论(0) 推荐(0) 编辑
摘要:过滤组件的使用 有三种使用方式 方式一:使用drf内置过滤类 第一步:保证视图类继承 GenericAPIView 及其子类 # 以图书类为例 class BookView(ViewSetMixin, ListAPIView): queryset = Book.objects.all() seria 阅读全文
posted @ 2023-12-28 15:50 wellplayed 阅读(33) 评论(0) 推荐(0) 编辑
摘要:排序组件快速使用 第一步:视图类需继承GenericAPIView或其子类 # 以图书类为例 class BookView(ViewSetMixin, ListAPIView): queryset = Book.objects.all() serializer_class = BookSeriali 阅读全文
posted @ 2023-12-28 15:27 wellplayed 阅读(33) 评论(0) 推荐(0) 编辑
摘要:频率组件的书写 例:书写频率组件:一分钟只能访问5次(CommonThrottle) 第一步:新建一个py文件(以throttling为例) 第二步:书写频率类,并继承继承SimpleRateThrottle # 首先导入模块 from rest_framework.throttling impor 阅读全文
posted @ 2023-12-27 15:30 wellplayed 阅读(15) 评论(0) 推荐(0) 编辑
摘要:权限组件的书写 例:书写权限组件(CommonPermission) 第一步:新建一个py文件(以permission为例) 第二步:书写认证类,并继承继承BasePermission # 首先导入模块 from rest_framework.permissions import BasePermi 阅读全文
posted @ 2023-12-27 15:19 wellplayed 阅读(10) 评论(0) 推荐(0) 编辑
摘要:认证组件的书写 例:书写登录认证(LoginAuth) 第一步:新建一个py文件(以auth为例) 第二步:书写认证类,并继承BaseAuthentication # 首先导入模块 from rest_framework.authentication import BaseAuthenticatio 阅读全文
posted @ 2023-12-26 16:25 wellplayed 阅读(7) 评论(0) 推荐(0) 编辑
摘要:自动生成路由 第一步:导入 from rest_framework.routers import SimpleRouter, DefaultRouter 第二步:实例化 router = SimpleRouter() 第三步:注册路径(以BookView为例) router.register('bo 阅读全文
posted @ 2023-12-26 16:12 wellplayed 阅读(10) 评论(0) 推荐(0) 编辑
摘要:方案一:在表模型(models)中写,在序列化类中映射 模型层书写: def publish_detail(self): return {'name': self.publish.name, 'city': self.publish.city} 序列化类(serializer)中书写: publis 阅读全文
posted @ 2023-12-21 14:58 wellplayed 阅读(39) 评论(0) 推荐(0) 编辑
摘要:我们可以使用使用GenericAPIView+序列化类+Response写接口 了解GenericAPIView 类属性: queryset:要序列化的所有数据 serializer_class:序列化类 lookup_field = 'pk' :查询单条时的key值,默认为pk,可以修改名称如'i 阅读全文
posted @ 2023-11-24 09:53 wellplayed 阅读(57) 评论(0) 推荐(0) 编辑
摘要:状态码 为了方便设置状态码,REST framewrok在rest_framework.status模块中提供了常用状态码常量。 导入: from rest_framework import status 提示: 默认响应状态码200 1)信息告知 - 1xx HTTP_100_CONTINUE(继 阅读全文
posted @ 2023-11-23 21:17 wellplayed 阅读(23) 评论(0) 推荐(0) 编辑
摘要:drf之——请求 Request类对象的分析 1).data request.data 返回解析之后的请求体数据。类似于Django中标准的request.POST和 request.FILES属性,但提供如下特性: 包含了解析之后的文件和非文件数据 包含了对POST、PUT、PATCH请求方式解析 阅读全文
posted @ 2023-11-23 20:47 wellplayed 阅读(5) 评论(0) 推荐(0) 编辑
摘要:1.书写序列化类,继承ModelSerializer from rest_framework import serializers class BookSerializer(serializers.ModelSerializer): 2.在序列化类中在写一个类(固定写法): class Meta: 阅读全文
posted @ 2023-11-21 21:35 wellplayed 阅读(51) 评论(0) 推荐(0) 编辑
摘要:序列化的字段有:name,price ,publish_detail,author_list 反序列化字段:name,price ,publish,author 反序列化之保存 代码书写: 视图类: class BookView(APIView): def post(self, request): 阅读全文
posted @ 2023-11-21 21:24 wellplayed 阅读(25) 评论(0) 推荐(0) 编辑
摘要:方式一:在序列化类中书写 想要输出以下字段: ——(书名,价格,出版社信息(id,出版社名,出版社地址)) 效果如下图: 需要在序列化类下书写以下代码: class BookSerializer(serializers.Serializer): name = serializers.CharFiel 阅读全文
posted @ 2023-11-21 20:59 wellplayed 阅读(15) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示