[英]How to fix " AttributeError at /api/doc 'AutoSchema' object has no attribute 'get_link' " error in Django
We are practicing an example of REST API on the Internet.我们正在网上练习一个REST API的例子。
However, the following error occurred.但是,发生了以下错误。
I tried a way in this link, but the situation hasn't changed.我在这个链接中尝试了一种方法,但情况没有改变。
why swagger raises unclear error - Django 为什么 swagger 引发不明确的错误 - Django
from django.contrib import admin
from django.conf.urls import url, include
from rest_framework import routers
from rest_framework_swagger.views import get_swagger_view
import consumer.api
app_name = 'consumer'
router = routers.DefaultRouter()
router.register('consumers', consumer.api.ConsumerViewSet)
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^api/doc', get_swagger_view(title='Rest API Document')),
url(r'^api/v1/', include((router.urls, 'consumer'), namespace='api')),
]
Exception Type: AttributeError at /api/doc
Exception Value: 'AutoSchema' object has no attribute 'get_link'
它对我有用,当我将下面添加到Settings.py
REST_FRAMEWORK = { 'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.coreapi.AutoSchema' }
As mentioned by @vctrd , this error may be due to CoreAPI support being deprecated in favor of OpenAPI since DRF 3.10 :正如@vctrd所提到的,此错误可能是由于自DRF 3.10起不推荐使用 CoreAPI 支持以支持 OpenAPI:
Since we first introduced schema support in Django REST Framework 3.5, OpenAPI has emerged as the widely adopted standard for modeling Web APIs.自从我们在 Django REST Framework 3.5 中首次引入模式支持以来,OpenAPI 已成为广泛采用的 Web API 建模标准。
This release begins the deprecation process for the CoreAPI based schema generation, and introduces OpenAPI schema generation in its place.此版本开始了基于 CoreAPI 的模式生成的弃用过程,并在其位置引入了 OpenAPI 模式生成。
You'll still be able to keep using CoreAPI schemas, API docs, and client for the foreseeable future.在可预见的未来,您仍然可以继续使用 CoreAPI 模式、API 文档和客户端。 We'll aim to ensure that the CoreAPI schema generator remains available as a third party package, even once it has eventually been removed from REST framework, scheduled for version 3.12.我们的目标是确保 CoreAPI 模式生成器作为第三方包仍然可用,即使它最终从 REST 框架中删除,计划在 3.12 版中使用。
As such, the generation of API schemas seems to have been made more convenient in the recent DRF versions if you use OpenAPI instead of Core API.因此,如果您使用 OpenAPI 而不是 Core API,则在最近的 DRF 版本中,API 模式的生成似乎更加方便。 The documentation of DRF OpenAPI schema generation can be found at: https://www.django-rest-framework.org/api-guide/schemas/可以在以下位置找到 DRF OpenAPI 模式生成的文档: https ://www.django-rest-framework.org/api-guide/schemas/
But if it is desirable to use Core API instead for whatever reason, @Omkar's answer will be the solution.但是,如果出于某种原因需要使用 Core API,@Omkar 的答案将是解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.