简体   繁体   English

如何修复 Django 中的“/api/doc 'AutoSchema' 对象没有属性 'get_link' 属性错误”错误

[英]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.

相关问题 如何修复“列表”object 在 Python/Django 中没有属性“get”(AttributeError) - How to fix 'list' object has no attribute 'get' (AttributeError) in Python/Django python django:如何修复错误说:AttributeError at / 'Home' object has no attribute 'get' - python django: How to fix error saying: AttributeError at / 'Home' object has no attribute 'get' 如何修复'AttributeError:'NoneType'对象在python中没有属性'get'错误 - How to fix 'AttributeError: 'NoneType' object has no attribute 'get' error in python Django错误:/ link /'LinkForm'对象上的AttributeError没有属性'url' - Django error: AttributeError at /link/ 'LinkForm' object has no attribute 'url' 无法修复 Python 错误 AttributeError: 'int' object 没有属性 'get' - Cant fix Python error AttributeError: 'int' object has no attribute 'get' Django 3.2 如何修复 AttributeError: 'itertools.chain' object has no attribute 'filter' 错误? - Django 3.2 how to fix AttributeError: 'itertools.chain' object has no attribute 'filter' error? 尝试使用 Django 设置 SendGrid 的电子邮件 API 时出现此错误 - AttributeError: 'str' object has no attribute 'get' - Getting this error while trying to set SendGrid's email API with Django - AttributeError: 'str' object has no attribute 'get' 如何修复 AttributeError: 'str' object has no attribute 'get_side' - How to fix AttributeError: 'str' object has no attribute 'get_side' “如何解决'AttributeError:'NoneType'对象在Python中没有属性'tbody'错误? - "How to fix 'AttributeError: 'NoneType' object has no attribute 'tbody'' error in Python? 如何修复错误:AttributeError: 'str' object has no attribute 'text' - How to fix error: AttributeError: 'str' object has no attribute 'text'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM