DRF Quick Start
⭐It's almost 2023, and there are still people who write their own codes for adding, deleting, modifying and checking? ! ? I don't allow anyone to use DRF yet!
⭐Today I will teach you to use Django Rest FrameWork to automatically generate Restful-style CRUD codes and interface documents!
⭐Reference article: Homepage - Django REST Framework (django-rest-framework.org) (official document)
- Notice:
- Django Rest FrameWork is used under the Django framework
- The following content needs to create a Django project first
- What is the Restful style: RESTful API is a popular API design style
install dependencies
Install
Rest FrameWork needs to meet the following conditions:
- Python (3.6, 3.7, 3.8, 3.9, 3.10)
- Django (2.2, 3.0, 3.1, 3.2, 4.0, 4.1)
# 安装Django
pip install django
# 安装DRF
pip install djangorestframework
# 安装自动生成接口文档需要的依赖
pip install coreapi
set up
- The following
seeting.py
are configured
INSTALLED_APPS = [
...
'rest_framework', # 注册app
'myapp', # 记得注册自己的app
]
# 数据库记得自行配置
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': '***',
'USER': '***',
'PASSWORD': '***',
'HOST': '127.0.0.1',
'PORT': '3306',
}
}
quick use
configure models
models.py
The configuration of the following content in the app
from django.db import models
class Student(models.Model):
# 模型字段
name = models.CharField(max_length=100, verbose_name="姓名")
sex = models.BooleanField(default=1, verbose_name="性别")
age = models.IntegerField(verbose_name="年龄")
class_null = models.CharField(max_length=5, verbose_name="班级编号")
description = models.TextField(max_length=1000, verbose_name="个性签名")
class Meta:
# 表名称
db_table = "tb_student"
# 表详细名称
verbose_name_plural = "学生"
- Configure the models and remember to migrate the database
python manage.py makemigrations
python manage.py migrate
Configure serializers
- The following content creates a new
serializers.py
directory
from rest_framework import serializers
from app.models import Student
# 创建序列化器类,回头会在试图中被调用
class StudentModelSerializer(serializers.ModelSerializer):
class Meta:
# 导入对应models
model = Student
# 选择生成对应models全部的字段
fields = "__all__"
# 可选择操作的字段
# fields = ['name', 'sex']
Configure view
- The following content
views.py
is configured
from rest_framework.viewsets import ModelViewSet
from .models import Student
from .serializers import StudentModelSerializer
# 生成代码 配置一个类即可
class StudentViewSet(ModelViewSet):
queryset = Student.objects.all()
serializer_class = StudentModelSerializer
configure url
- The following content
urls.py
is configured
from django.urls import path
from . import views
from rest_framework.routers import DefaultRouter
from rest_framework.documentation import include_docs_urls
# 路由列表
urlpatterns = [
# 配置api文档路由 title配置API文档的标题
path('docs/', include_docs_urls(title='API document')),
]
router = DefaultRouter() # 可以处理视图的路由器
router.register('students', views.StudentViewSet) # 向路由器中注册视图集
urlpatterns += router.urls # 将路由器中的所以路由信息追到到django的路由列表中
- The following content
urls.py
is configured
from django.contrib import admin
from django.urls import path, include
from app import urls
urlpatterns = [
path('admin/', admin.site.urls),
path('stu/', include(urls)),
]
configure seetings
- The following content
seetings.py
is configured
# 配置默认API文档
REST_FRAMEWORK = {
# coreapi接口文档
'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.AutoSchema',
}
Startup project
# 直接启动项目就可用访问啦
python manage.py runserver
- http://127.0.0.1:8000/stu/ Access the apiroot interface
- http://127.0.0.1:8000/stu/students/ Access to the operation interface of database students
- http://127.0.0.1:8000/stu/docs/ access interface documentation
Page preview:
-
interface documentation
-
api management page
⭐The above are some basic configuration content! The basic CURD code has been generated!
⭐Of course DRF can also configure a lot of things JWT, pagination... (will be introduced in later articles)
⭐The above content is just a quick configuration of the comprehensive API, please refer to the official document: Home - Django REST Framework (django-rest-framework.org)
⭐If it is helpful to you, please give me a thumbs up