主要记录drf-yasg Swagger依赖出现的一些问题
其中就包含:Django rest framework 返回错误信息 str returned non-string (type NoneType)
相关的配置和安装:
安装
pip install -U drf-yasg
pip install -U drf-yasg[指定版本]
配置
在项目的settings.py文件下配置
INSTALLED_APPS = [
...
'django.contrib.staticfiles', # required for serving swagger ui's css/js files
'drf_yasg',
...
]
在项目文件的urls.p文件下配置
...
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="Snippets API",
default_version='v1',
description="Test description",
terms_of_service="https://www.google.com/policies/terms/",
contact=openapi.Contact(email="contact@snippets.local"),
license=openapi.License(name="BSD License"),
),
public=True,
permission_classes=[permissions.AllowAny],
)
urlpatterns = [
path('swagger/', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
path('redoc/', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'),
]
相关问题
其中就包含:Django rest framework 返回错误信息 str returned non-string (type NoneType)
在请求访问/swagger/时报错, 主要原因是自己重新定义了User模型
需要在自己定义的User模型内添加str返回可解决,或者不要进入/admin登录也可解决
class User(BaseModel, AbstractUser):
...
def __str__(self):
return self.username