6

I'm trying to add https://github.com/axnsan12/drf-yasg this library to our django application and getting the below error.

permission_classes=(permissions.AllowAny,),
  File "/env/lib/python3.7/site-packages/drf_yasg/views.py", line 67, in get_schema_view
    _generator_class = generator_class or swagger_settings.DEFAULT_GENERATOR_CLASS
  File "env/lib/python3.7/site-packages/drf_yasg/app_settings.py", line 122, in __getattr__
    val = perform_import(val, attr)
  File "env/lib/python3.7/site-packages/rest_framework/settings.py", line 166, in perform_import
    return import_from_string(val, setting_name)
  File "env/lib/python3.7/site-packages/rest_framework/settings.py", line 180, in import_from_string
    raise ImportError(msg)
ImportError: Could not import 'drf_yasg.generators.OpenAPISchemaGenerator' for API setting 'DEFAULT_GENERATOR_CLASS'. ImportError: cannot import name 'URLPattern' from 'rest_framework.compat' (env/lib/python3.7/site-packages/rest_framework/compat.py).

after some research I found some people suggested installing this package to solve the issue

pip3 install packaging 

But this is not making any difference. Any other good api documentation library available for django rest api?

Krishnadas PC
  • 5,981
  • 2
  • 53
  • 54

4 Answers4

13

UPDATE

The issue has been fixed in drf-yasg==1.20.0--(Release notes) so that you can upgrade to the latest version by

pip install drf-yasg -U

Original answer

There is an open issue in drf-yasg-(#641) to fix this issue. Unfortunately, the project is no longer maintained given that there has been no activity since Feb 2020 (Ref this).

But, @JoelLefkowitz created a fork of the project and it is available in named drf-yasg2 which has been fixed the OP's issue.

What you need to do is, migrate from drf-yasg to drf-yasg2--(ReadTheDoc)

Installation

pip install drf-yasg2

and add it to the INSTALLED_APPS section

INSTALLED_APPS = [
      ...
      'drf_yasg2',
      ...
   ]
JPG
  • 82,442
  • 19
  • 127
  • 206
  • I have used the ones given in https://www.django-rest-framework.org/topics/documenting-your-api/ without additional `pip` using `openapi` shchemas. But newly added apis are not displayed in openapi url or swagger ui only the old ones. any extra step we need after adding new apis? – Krishnadas PC Oct 23 '20 at 04:52
  • I am not sure what you have got. It is better to ask a new question regarding the new issue. – JPG Oct 23 '20 at 04:54
  • @KrishnadasPC I think that you can upgrade `drf-yasg==1.20.0`, since this issue was fixed in a new release (https://github.com/axnsan12/drf-yasg/issues/641) – machin Oct 28 '20 at 07:29
  • @machin I have updated the answer with the reference. Thanks for the update!!! – JPG Oct 28 '20 at 07:38
2

Looks like DRF does not have rest_framework.compat.URLPattern starting from version 3.12, and drf-yasg have not caught up on this yet. Make sure that your DRF version is less than 3.12. For example: pip install djangorestframework==3.11.2

Eugene Prikazchikov
  • 1,794
  • 1
  • 13
  • 11
  • But we have developed some of the API's and I think downgrading is not a good solution at this stage. – Krishnadas PC Oct 21 '20 at 08:08
  • 2
    If you check https://github.com/axnsan12/drf-yasg, it says that it supports only following versions of DRF - 3.8, 3.9, 3.10, 3.11. If your code requires other version of DRF, than I am afraid drf-yasg will not work for you. Try downgrading, most likely it won't cause your code to break. – Eugene Prikazchikov Oct 21 '20 at 08:13
  • Erm... you generally don't downgrade a core application library for a documentation generator. –  Oct 21 '20 at 08:44
  • Typically yes, unless you really want that generator, and downgrade is minor one. This craft all about trade-offs :). Another option might be fixing drf-yasg and submitting PR, using in the app your patched version until the fix is merged/released. – Eugene Prikazchikov Oct 21 '20 at 09:12
1

The following worked for me

pip install drf-yasg -U
knoxgon
  • 1,070
  • 2
  • 15
  • 31
0

It could surely help you guys write this in settings.py file:

SWAGGER_SETTINGS = { "DEFAULT_GENERATOR_CLASS": "rest_framework.schemas.generators.BaseSchemaGenerator", }