如何修复 Django 中的“AttributeError at /api/doc 'AutoSchema' object has no attribute 'get_link'”错误

How to fix " AttributeError at /api/doc 'AutoSchema' object has no attribute 'get_link' " error in Django

正在网上练习REST的例子API

但是,出现以下错误。

我在这个link中尝试了一个方法,但是情况没有改变。

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' }

, this error may be due to CoreAPI support being deprecated in favor of OpenAPI since DRF 3.10所述:

Since we first introduced schema support in Django REST Framework 3.5, OpenAPI has emerged as the widely adopted standard for modeling Web APIs.

This release begins the deprecation process for the CoreAPI based schema generation, and introduces OpenAPI schema generation in its place.

You'll still be able to keep using CoreAPI schemas, API docs, and client for the foreseeable future. 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.

因此,如果您使用 OpenAPI 而不是 Core API,那么在最近的 DRF 版本中似乎可以更方便地生成 API 模式。 DRF OpenAPI 模式生成的文档可以在以下位置找到:https://www.django-rest-framework.org/api-guide/schemas/

但是如果出于任何原因需要使用 Core API,@Omkar 的回答将是解决方案。