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

2

63

We are practicing an example of REST API on the Internet.

However, the following error occurred.

I tried a way in this link, but the situation hasn't changed.

why swagger raises unclear error - 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'
Fumigator answered 26/8, 2019 at 8:22 Comment(4)
Could you provide a link to that example?Malena
django-rest-swagger is no longer being maintained, they suggest switching to drf-yasgReduce
Please post your viewset class, there may be an error there.Cockspur
Note that drf-yasg is also apparently not being maintained any longer (Since Feb 2020). Refer github.com/axnsan12/drf-yasg/issues/641Predestine
B
155

It worked for me, When I added below into Settings.py

REST_FRAMEWORK = { 'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.coreapi.AutoSchema' }
Blockbusting answered 19/9, 2019 at 6:43 Comment(2)
the coreAPI schema generation is being deprecated, you can now generate a openAPI schema following this quick start: django-rest-framework.org/community/3.10-announcementPopham
@Popham This should be a separate answer or included in this one.Justify
E
6

As mentioned by @vctrd, 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.

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. The documentation of DRF OpenAPI schema generation can be found at: 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.

Essequibo answered 28/4, 2021 at 22:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.