14

Previously I was using drf-yasg but want to update to use OpenAPI 3. I am trying to switch over to drf-spectacular. Following the instruction, I ran pip install drf-spectacular, I've removed all references to the drf-yasg package, and updated Settings.py as follows:

INSTALLED_APPS = [ 
    ...
    "drf_spectacular",
]


REST_FRAMEWORK = {
    "DEFAULT_SCHEMA_CLASS": "drf_spectacular.openapi.AutoSchema",
}

When I use the CLI to generate the schema, I get the bellow AssertionError. If anyone has run into this problem before and has any insight, it would be much appreciated!

I'm using Python 3.7, Django 3.0, Django Rest Framework 3.11, and DRF Spectacular 0.10.0.

Traceback (most recent call last):
  File "manage.py", line 23, in <module>
    main()
  File "manage.py", line 19, in main
    execute_from_command_line(sys.argv)
  File "/opt/anaconda3/envs/dev/lib/python3.7/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/opt/anaconda3/envs/dev/lib/python3.7/site-packages/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/opt/anaconda3/envs/dev/lib/python3.7/site-packages/django/core/management/base.py", line 328, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/opt/anaconda3/envs/dev/lib/python3.7/site-packages/django/core/management/base.py", line 369, in execute
    output = self.handle(*args, **options)
  File "/opt/anaconda3/envs/dev/lib/python3.7/site-packages/drf_spectacular/management/commands/spectacular.py", line 50, in handle
    schema = generator.get_schema(request=None, public=True)
  File "/opt/anaconda3/envs/dev/lib/python3.7/site-packages/drf_spectacular/generators.py", line 187, in get_schema
    paths=self.parse(request, public),
  File "/opt/anaconda3/envs/dev/lib/python3.7/site-packages/drf_spectacular/generators.py", line 160, in parse
    'Incompatible AutoSchema used on View. Is DRF\'s DEFAULT_SCHEMA_CLASS '
AssertionError: Incompatible AutoSchema used on View. Is DRF's DEFAULT_SCHEMA_CLASS pointing to "drf_spectacular.openapi.AutoSchema" or any other drf-spectacular compatible AutoSchema?
JBWilkinson
  • 141
  • 1
  • 3

5 Answers5

2

Please update the Django Rest Framework 3.11 to 3.12 it will work.

1

I have the same issue when I had REST_FRAMEWORK two times written for different settings in my settings.py. I moved everything in one variable and error is gone

Body
  • 11
  • 2
  • For me it was even enough to have an import `from rest_framework.pagination import …` to trip it up. Glad I found this – panepeter Nov 19 '22 at 12:52
1

If the AssertionError is about DRF's ObtainAuthToken then it is most likely an old bug in DRF. This issue was fixed in DRF>=3.12. Prior to that, DRF used a wrong class where it was not supposed to.

drf-yasg seems to not suffer from this upstream bug due to a different injection technique used. drf-spectacular has a mitigation for the bug starting with 0.24.0.

Related GH issue with workaround for older drf-spectacular versions: https://github.com/tfranzel/drf-spectacular/issues/796#issuecomment-1231464792

Sidenote: If this does not fix your problem and/or it is the same Assertion for some other view, you likely have a misconfigured settings.py. Make sure DEFAULT_SCHEMA_CLASS is properly set as stated in the README. Also make sure you are not shooting yourself in the foot by not setting this also in your production settings file. If the problem still persists, please open an issue on Github and get help there.

Insa
  • 1,610
  • 12
  • 17
0

Check if you're importing from rest_framework!

Having a

from rest_framework.pagination import PageNumberPagination

was all it took to provoke the error. As soon as I removed the import, things started working again.

This even applies when importing other modules which in turn import from rest_framework. I ended up using the "sting imports" like so:

"DEFAULT_PAGINATION_CLASS": "api.pagination.DefaultPagination",
panepeter
  • 3,224
  • 1
  • 29
  • 41
0

I had the same issue. Just make sure you don't have REST_FRAMEWORK defined more than once in your settings.py file.