我正在尝试设置 drf-spectaulous 来为我的 djangorestframework API 生成 AutoSchema。
我已经按照自述文件中的说明进行了设置,例如,在已安装的应用程序中,版本 =“~=0.11.1”,其余框架设置如下:
REST_FRAMEWORK = {
...,
'DEFAULT_SCHEMA_CLASS': ('drf_spectacular.openapi.AutoSchema',),
}
我的djangorestframework是3.12版本。 当我运行以下命令时出现错误:
./manage.py spectacular --file schema.yml
这是我的示例视图:
class LinkListView(ListView):
permission_classes = [AllowAny]
template_name = "core/linklist.html"
context_object_name = "link_list"
def get_queryset(self):
return Org.objects.all()
在阅读了大量有关模式的内容后,我认为我有一些其他库与此冲突,或者之前的一些其他 swagger 替代方案存在冲突。根据错误,这有意义吗?有什么我想尝试的吗?
谢谢!
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 "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 371, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python3.8/site-packages/drf_spectacular/management/commands/spectacular.py", line 50, in handle
schema = generator.get_schema(request=None, public=True)
File "/usr/local/lib/python3.8/site-packages/drf_spectacular/generators.py", line 188, in get_schema
paths=self.parse(request, public),
File "/usr/local/lib/python3.8/site-packages/drf_spectacular/generators.py", line 130, in parse
self._initialise_endpoints()
File "/usr/local/lib/python3.8/site-packages/drf_spectacular/generators.py", line 113, in _initialise_endpoints
self.endpoints = self.inspector.get_api_endpoints()
File "/usr/local/lib/python3.8/site-packages/drf_spectacular/generators.py", line 22, in get_api_endpoints
api_endpoints = self._get_api_endpoints(patterns, prefix)
File "/usr/local/lib/python3.8/site-packages/drf_spectacular/generators.py", line 55, in _get_api_endpoints
nested_endpoints = self._get_api_endpoints(
File "/usr/local/lib/python3.8/site-packages/drf_spectacular/generators.py", line 49, in _get_api_endpoints
if self.should_include_endpoint(path, callback):
File "/usr/local/lib/python3.8/site-packages/rest_framework/schemas/generators.py", line 120, in should_include_endpoint
if callback.cls.schema is None:
File "/usr/local/lib/python3.8/site-packages/rest_framework/schemas/inspectors.py", line 120, in __get__
assert issubclass(inspector_class, ViewInspector), (
TypeError: issubclass() arg 1 must be a class
我的图书馆:
[packages]
django = "~=3.1"
psycopg2 = "*"
djangorestframework = "~=3.12"
django-extensions = "*"
python-dotenv = "~=0.9"
django-cors-headers = "*"
django-filter = "*"
freezegun = "*"
pip = "*"
pytz = "*"
packaging = "*"
python-jose = "~=3.0"
cryptography = "~=2.0.3"
requests = "*"
twine = "*"
pyjwt = "*"
gunicorn = "*"
whitenoise = "*"
dj-database-url = "*"
boto = "*"
drf-spectacular = "~=0.11.1"
这很可能只是一个小设置问题。您将
DEFAULT_SCHEMA_CLASS
定义为字符串的元组,而不是只是字符串。 Django 可能将其解析为一个列表/元组,其中一个类作为项目。那么错误消息就有意义了,因为元组不是类。我希望这能解决你的问题。事情应该是这样的:
REST_FRAMEWORK = {
# YOUR SETTINGS
'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema',
}