TypeError函数SwaggerSchemaView不是JSON可序列化的

时间:2017-06-30 19:55:02

标签: django swagger

/ docs /中的TypeError不是JSON可序列化的

这是我的代码,导致了这个,

def schema_view(request):
  
        schema_view = get_swagger_view(title='Docs API')
        return Response(schema_view)

这是错误日志

的回溯

Environment:

Request Method: GET
Request URL: http://localhost:8000/test/docs/

Django Version: 1.10.1
Python Version: 2.7.9
Installed Applications:
['corsheaders',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.sites',
 'test_app',
 'rest_framework',
 'rest_framework.authtoken',
 'rest_auth',
 'rest_auth.registration',
 'allauth',
 'allauth.account',
 'allauth.socialaccount',
 'allauth.socialaccount.providers.google',
 'allauth.socialaccount.providers.facebook',
 'allauth.socialaccount.providers.twitter',
 'rest_framework_swagger']
Installed Middleware:
['corsheaders.middleware.CorsMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "/Users/user/projects/test/testx/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner
  39.             response = get_response(request)

File "/Users/user/projects/test/testx/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  217.                 response = self.process_exception_by_middleware(e, request)

File "/Users/user/projects/test/testx/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  215.                 response = response.render()

File "/Users/user/projects/test/testx/lib/python2.7/site-packages/django/template/response.py" in render
  109.             self.content = self.rendered_content

File "/Users/user/projects/test/testx/lib/python2.7/site-packages/rest_framework/response.py" in rendered_content
  72.         ret = renderer.render(self.data, accepted_media_type, context)

File "/Users/user/projects/test/testx/lib/python2.7/site-packages/rest_framework/renderers.py" in render
  703.         context = self.get_context(data, accepted_media_type, renderer_context)

File "/Users/user/projects/test/testx/lib/python2.7/site-packages/rest_framework/renderers.py" in get_context
  661.             'content': self.get_content(renderer, data, accepted_media_type, renderer_context),

File "/Users/user/projects/test/testx/lib/python2.7/site-packages/rest_framework/renderers.py" in get_content
  411.         content = renderer.render(data, accepted_media_type, renderer_context)

File "/Users/user/projects/test/testx/lib/python2.7/site-packages/rest_framework/renderers.py" in render
  105.             separators=separators

File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py" in dumps
  250.         sort_keys=sort_keys, **kw).encode(obj)

File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py" in encode
  209.             chunks = list(chunks)

File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py" in _iterencode
  442.             o = _default(o)

File "/Users/user/projects/test/testx/lib/python2.7/site-packages/rest_framework/utils/encoders.py" in default
  70.         return super(JSONEncoder, self).default(obj)

File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py" in default
  184.         raise TypeError(repr(o) + " is not JSON serializable")

Exception Type: TypeError at /test/docs/
Exception Value: <function SwaggerSchemaView at 0x10ee8aa28> is not JSON serializable

PS:为什么stackoverflow不允许我发布这个,说它只是代码,请添加更多细节?我还应该添加什么?

1 个答案:

答案 0 :(得分:0)

我认为您使用Response中的restframeworkResponse对象需要一个可序列化的对象作为数据。您正在传递一个视图作为参数,这是不可序列化的。您可以尝试传递字典:

def schema_view(request):

    schema_view = get_swagger_view(title='Docs API')
    dic_data = {'schema_view': schema_view}
    return Response(dic_data)

http://www.django-rest-framework.org/api-guide/responses/