问题描述
我在我的 django 项目中使用了 django rest_framework_swagger
,一切正常,但是当我添加一些包含方法 Swagger 的 URL 时开始给我 500 internal server error
。
我不知道为什么会出现这个错误,我已经检查过但没有找到任何解决这个错误的方法。
我正在使用:
django 1.11.7
rest_framework_swagger 2.1.2
django rest framework 3.7.3
网址
from django.conf.urls import url,include
from link_one.views import LinkOneViewSet
from link_two.views import LinkTwoViewSet
schema_view = get_swagger_view(title='My Project APIs')
urlpatterns = [
url(r'^$',schema_view),url(r'^foo/(?P<foo_id>\w+)/bar/(?P<bar_id>\w+)/link1',LinkOneViewSet.as_view({'get': 'list'})),url(r'^foo/(?P<foo_id>\w+)/bar/(?P<bar_id>\w+)/link2',LinkTwoViewSet.as_view({'get': 'list'})),url(r'^foo/(?P<foo_id>\w+)/bar/(?P<bar_id>\w+)/link3',include('link_three.urls'))
]+ static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)
[25/Jan/2021 14:03:31] ERROR [django.request.exception:135] Internal Server Error: /
Traceback (most recent call last):
File "C:\Users\myuser\conda_env\lib\site-packages\django\core\handlers\exception.py",line 41,in inner
response = get_response(request)
File "C:\Users\myuser\conda_env\lib\site-packages\django\core\handlers\base.py",line 187,in _get_response
response = self.process_exception_by_middleware(e,request)
File "C:\Users\myuser\conda_env\lib\site-packages\django\core\handlers\base.py",line 185,in _get_response
response = wrapped_callback(request,*callback_args,**callback_kwargs)
File "C:\Users\myuser\conda_env\lib\site-packages\django\views\decorators\csrf.py",line 58,in wrapped_view
return view_func(*args,**kwargs)
File "C:\Users\myuser\conda_env\lib\site-packages\django\views\generic\base.py",line 68,in view
return self.dispatch(request,*args,**kwargs)
File "C:\Users\myuser\conda_env\lib\site-packages\rest_framework\views.py",line 489,in dispatch
response = self.handle_exception(exc)
File "C:\Users\myuser\conda_env\lib\site-packages\rest_framework\views.py",line 449,in handle_exception
self.raise_uncaught_exception(exc)
File "C:\Users\myuser\conda_env\lib\site-packages\rest_framework\views.py",line 486,in dispatch
response = handler(request,**kwargs)
File "C:\Users\myuser\conda_env\lib\site-packages\rest_framework_swagger\views.py",line 32,in get
schema = generator.get_schema(request=request)
File "C:\Users\myuser\conda_env\lib\site-packages\rest_framework\schemas\generators.py",line 278,in get_schema
links = self.get_links(None if public else request)
File "C:\Users\myuser\conda_env\lib\site-packages\rest_framework\schemas\generators.py",line 316,in get_links
link = view.schema.get_link(path,method,base_url=self.url)
File "C:\Users\myuser\conda_env\lib\site-packages\rest_framework\schemas\inspectors.py",line 179,in get_link
fields += self.get_serializer_fields(path,method)
File "C:\Users\myuser\conda_env\lib\site-packages\rest_framework\schemas\inspectors.py",line 302,in get_serializer_fields
serializer = view.get_serializer()
File "C:\Users\myuser\conda_env\lib\site-packages\rest_framework\generics.py",line 112,in get_serializer
return serializer_class(*args,**kwargs)
TypeError: 'list' object is not callable
解决方法
经过大量搜索和调试,我找到了解决方案。
解决方案是,不要为一个 ViewSet 使用多个序列化器类。
在我的一个视图集中,我正在这样做,这就是造成问题的原因。
class FooBarViewset(ModelViewSet):
serializer_class = [DefaultSerializer,BarSerializer,FooSerializer]
但我没有意识到这会导致错误。
这是我正在使用的修复
class FooBarViewset(ModelViewSet):
serializer_class = DefaultSerializer
您还可以使用 get_serializer 方法并将序列化器类与操作一起使用,请查看此答案 Django rest framework,use different serializers in the same ModelViewSet