安装配置 DRF
安装 DRF
打开终端,进入 django 开发的python 虚拟环境中,执行下面的命令安装 DRF:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| leazhi@ubuntuhome:~$ workon web12 (web12) leazhi@ubuntuhome:~$ pip3 install djangorestframework Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple Collecting djangorestframework Downloading https://pypi.tuna.tsinghua.edu.cn/packages/ff/4b/3b46c0914ba4b7546a758c35fdfa8e7f017fcbe7f23c878239e93623337a/djangorestframework-3.14.0-py3-none-any.whl (1.1 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.1/1.1 MB 3.2 MB/s eta 0:00:00 Requirement already satisfied: django>=3.0 in ./.env/web12/lib/python3.10/site-packages (from djangorestframework) (3.1.7) Requirement already satisfied: pytz in ./.env/web12/lib/python3.10/site-packages (from djangorestframework) (2023.3) Requirement already satisfied: asgiref<4,>=3.2.10 in ./.env/web12/lib/python3.10/site-packages (from django>=3.0->djangorestframework) (3.7.2) Requirement already satisfied: sqlparse>=0.2.2 in ./.env/web12/lib/python3.10/site-packages (from django>=3.0->djangorestframework) (0.4.4) Requirement already satisfied: typing-extensions>=4 in ./.env/web12/lib/python3.10/site-packages (from asgiref<4,>=3.2.10->django>=3.0->djangorestframework) (4.7.0) Installing collected packages: djangorestframework Successfully installed djangorestframework-3.14.0 (web12) leazhi@ubuntuhome:~$
|
注册 DRF
编辑项目目录下的 settings.py 文件,在 INSTALLED_APPS = []
中添加 DRF 的注册配置,如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
...
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'users.apps.UsersConfig', 'rest_framework', ] ...
|
DRF 的使用
序列化器 ModelSerializer 和 Serializer
1.序列化器的作用:
2.ModelSerializer 和 Serializer 的区别:
- ModelSerializer 需要知道模型类,他提供了增删改查的功能;
- Serializer 不需要知道模型类,他不提供增删改功能
ModelSerializer 序列化器
1.编辑子应用目录下的 views.py 文件,导入 rest_framework 中的 ModelViewSet 模块,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
...
from users.serializer import BookInfoSerializers, PeopleInfoSerializers from rest_framework.viewsets import ModelViewSet
...
class BookInfoViewAPIS(ModelViewSet):
queryset = BookInfo.objects.all()
serializer_class = BookInfoSerializers
|
2.编辑子应用目录下的路由文件 urls.py,在原有路由下面新些 DRF 路由代码(DRF 需要使用自带的路由格式,而不是使用 django 自带的路由),代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
...
from rest_framework.routers import DefaultRouter
...
urlpatterns = [ ... ]
router = DefaultRouter() router.register('bookers', viewset=views.BookInfoViewAPIS)
urlpatterns+= router.urls
|
3.在子应用 users 目录目录下创建序列化文件 serializer.py 文件,导入 serializers 模块,编写代码,实现模型类的序列化,内容为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
from rest_framework import serializers from .models import BookInfo, PeopleInfo
class BookInfoSerializers(serializers.ModelSerializer):
class Meta: model = BookInfo
fields = '__all__'
|
4.打开浏览器,访问路由 bookers ,如下图:
需要注意的是:修改、删除数据只需要在查询接口后面输入数据 id 即可操作!
Serializer 序列化器
1.编辑子应用目录下的 serializer.py 文件,最下面加入如下代码:
1 2 3 4 5 6 7 8 9 10
|
... class BookInfoSerializer(serializers.Serializer): id = serializers.IntegerField(label='ID', min_value=1) name = serializers.CharField(label='名称') pub_date = serializers.DateField(label='出版日期22')
|
2.编辑子应用目录下的 views.py 文件,最下面加入如下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
...
class BookInfoViewAPI(ModelViewSet):
queryset = BookInfo.objects.all()
serializer_class = BookInfoSerializer
|
3.编辑子应用目录下的 urls.py 文件,在 DRF 路由里面添加入如下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
...
router = DefaultRouter()
...
router.register('booker', viewset=views.BookInfoViewAPI)
urlpatterns+= router.urls
|
4.打开浏览器,访问路由 booker (注意却分路由 bookers ),如下:
注意:虽然这里也可以查询数据,有增、删、改的功能,但是不能使用。提交会报错!