1.组件引入
pip install rest_framework
pip install drf_yasg
2.setting文件INSTALLED_APPS添加
'rest_framework',
'drf_yasg'
3.SWAGGER_SETTINGS修改
'DEFAULT_INFO': 'import.path.to.urls.api_info',
4.配置静态文件存储 setting文件存储
BASE_DIR = Path(__file__).resolve().parent.parent
MEDIA_ROOT = os.path.join(BASE_DIR, 'BaseDate/IMAGE') # 配置静态存储路径
5.修改主程序的urls文件
from django.contrib import admin
from django.urls import path, include ,re_path #添加include引用
from rest_framework import permissions
from drf_yasg.views import get_schema_view
from drf_yasg import openapi
schema_view = get_schema_view(
openapi.Info(
title="TongChuangApi",
default_version='V1.1',
description='同创训练平台开发api', ),
public=True,
permission_classes=[permissions.AllowAny],
)
urlpatterns = [
path('admin/', admin.site.urls),
path('ImageMarks/', include(urls)), # 将include指向子app的路由配置文件
re_path(r'^swagger(?P<format>\.json|\.yaml)$', schema_view.without_ui(cache_timeout=0), name='schema-spec'),
path('doc/', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'), # <-- swagger配置页
path('redoc/', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc') # <-- redoc配置页
]
配置应用数据
1.创建接口应用
django-admin startApp ImageMarks
2.定义数据模型
from django.db import models
from django.contrib.auth.models import User
import time
import datetime
def SetImagePath(instance, filename):
ext = filename.split('.')[-1] # 后缀名
return '{}/{}.{}'.format(instance.CreateId.id, datetime.datetime.now(), ext)
class ImageInfo(models.Model):
ImgID = models.AutoField(primary_key=True, verbose_name='图片ID')
ImgName = models.CharField(verbose_name='图片名称', max_length=40)
ImgPath = models.ImageField(verbose_name='图片地址', upload_to=SetImagePath)
# ImgPath = models.ImageField(verbose_name='图片地址', upload_to='Image')
CreateTimer = models.DateTimeField(verbose_name="上传时间", auto_now_add=True) # 创建时更新
# CreateId = models.CharField(verbose_name='上传人', max_length=32)
CreateId = models.ForeignKey(User, on_delete=models.CASCADE, related_name='profile')
UpdateTimer = models.DateTimeField(verbose_name="更新时间", auto_now=True) # 修改时更新
UpdateId = models.CharField(verbose_name='更新人', max_length=32)
class Meta:
db_table = 'TC_ImageInfo' # 指明数据库表名
verbose_name = '图片信息' # 在admin站点中显示的名称
verbose_name_plural = verbose_name # 显示的复数名称
def __str__(self):
"""定义每个数据对象的显示信息"""
return self.ImgName
2.定义序列化函数
新建Serializers.py文件
class ImageInfoSerializer(serializers.ModelSerializer):
class Meta:
model = models.ImageInfo
fields = ('ImgID', 'ImgName', 'ImgPath', 'CreateTimer', 'CreateId', 'UpdateTimer', 'UpdateId')
3.定义视图文件
from rest_framework import viewsets
from . import Serializers
from . import models
class ImageMarksViews(viewsets.ModelViewSet):
queryset = models.ImageInfo.objects.all()
serializer_class = Serializers.ImageInfoSerializer
4.修改urls文件配置
from django.urls import path,include
from . import views
from rest_framework import routers
router = routers.DefaultRouter()
router.register(r'ImageMarks', views.ImageMarksViews, basename='ImageMarks')
urlpatterns = [
path("", include(router.urls))
]
启动应用
python manage.py runservier