LoginSignup
14
7

More than 3 years have passed since last update.

AttributeError: 'AutoSchema' object has no attribute 'get_link' と出てきてDRFのAPIドキュメント生成が使えない場合の対処

Last updated at Posted at 2019-10-10

はじめに

Django REST framework(DRF)ではかんたんな手順でAPIスキーマをきれいなUIで確認する事ができます。
こういう感じでurls.pyに記述するだけで実装できます.

urls.py
from django.urls import path, include
from rest_framework.documentation import include_docs_urls 

docs_view = include_docs_urls(
    title='my drf API',
    description='This is an awesome API',
)

urlpatterns = [
    path('docs/', docs_view),
]

エラーがでた

docs/にアクセスして確認してみると
AttributeError: 'AutoSchema' object has no attribute 'get_link'
と表示されエラーになりました.なんでや

環境

Python 3.7
django 2.2.5
djangorestframework 3.10.3
coreapi 2.3.3

DRF 3.10以上だと発生するエラー

3.10 Announcement
こちらによるとdrf3.10からはSchema生成にCoreAPIからOpenAPIを用いるようになり,将来的にはバージョン3.12でCoreAPIはDRFから削除される方針とのことです.
今回利用したモジュールはCoreAPIを使っているんですね.

OpenAPIのかわりにCoreAPIをつかうためにはsettings.pyに以下を書き加えます.

settings.py
REST_FRAMEWORK = {
  ...
  'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.coreapi.AutoSchema'
}

動いた!!!

まとめ

drf3.10以降で
AttributeError: 'AutoSchema' object has no attribute 'get_link'とエラーが出たり,CoreAPIが必要なモジュールを使うときは以下を記述する.

settings.py
REST_FRAMEWORK = {
  ...
  'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.coreapi.AutoSchema'
}

最後までお読みいただきありがとうございます.

14
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
7