当前位置:网站首页>DRF学习笔记(准备)
DRF学习笔记(准备)
2022-07-27 14:36:00 【fresh_nam】
介绍
drf即Django REST framework,它提供了定义序列化器Serializer的方法,可以快速根据 Django ORM 或者其它库自动序列化/反序列化。
好处:django可以渲染页面和返回数据,但在特定的场景下仅需要返回数据即可,drf则提供了各种方法方便我们实现数据的增删改查。
在开发REST API接口时,视图中做的最主要有三件事:
1.将请求的数据(如JSON格式)转换为模型类对象
2.操作数据库
3.将模型类对象转换为响应的数据(如JSON格式)
一、准备
环境:
操作系统:windows10
python: 3.7
django: 3.1.2
建议编辑器:pychram
建议
有django基础
二、引入库
1、安装drf
pip install djangorestframework
2、添加rest_framework
在创建的django项目里面的settings.py添加rest_framework应用:
INSTALLED_APPS = [
...
'rest_framework',
]
3、数据库配置
使用默认数据库应该没问题(我没用过),这里将使用mysql数据库,将数据库设置修改如下:
DATABASES = {
'default': {
'ENGINE':'django.db.backends.mysql',
'NAME': 'db_name', // 你的数据名
'USER': 'username', // 你的用户名
'PASSWORD': 'password', // 数据库密码
'HOST': '127.0.0.1', // 数据库所在位置
'PORT': '3306' // 端口
}
}
准备工作完成,可以开始写接口了!
下一章:DRF学习笔记(一):数据序列化
边栏推荐
- 【剑指offer】面试题55 - Ⅰ/Ⅱ:二叉树的深度/平衡二叉树
- Go language slow start - Basic built-in types
- 【剑指offer】面试题42:连续子数组的最大和——附0x80000000与INT_MIN
- 【云享读书会第13期】视频文件的编码格式
- Binder initialization process
- 实体类(VO,DO,DTO)的划分
- Binder初始化过程
- [daily question 1] 558. Intersection of quadtrees
- Jump to the specified position when video continues playing
- On juicefs
猜你喜欢

网络层的IP协议

C语言:自定义类型

C语言:数据的存储

Interview focus - TCP protocol of transport layer

Division of entity classes (VO, do, dto)

flutter —— 布局原理与约束

NPM install error unable to access

面试重点——传输层的TCP协议

First understanding of structure
![[sword finger offer] interview question 54: the k-largest node of the binary search tree](/img/13/7574af86926a228811503904464f3f.png)
[sword finger offer] interview question 54: the k-largest node of the binary search tree
随机推荐
Three uses of static keyword
Set the position of the prompt box to move with the mouse, and solve the problem of incomplete display of the prompt box
【云享读书会第13期】视频文件的编码格式
【剑指offer】面试题54:二叉搜索树的第k大节点
[regular expression] matches multiple characters
面试重点——传输层的TCP协议
js操作dom节点
flutter —— 布局原理与约束
Half find
[正则表达式] 匹配分组
[正则表达式] 单个字符匹配
Multimap case
【剑指offer】面试题42:连续子数组的最大和——附0x80000000与INT_MIN
[Yunxiang book club issue 13] packaging format and coding format of audio files
Catalog component design and custom extended catalog implementation in spark3
低代码是开发的未来吗?浅谈低代码平台
Modify spark to support remote access to OSS files
使用Lombok导致打印的tostring中缺少父类的属性
C language: minesweeping games
Go language slow start - package