Django Rest Framework视图中的ContentNotRenderedError

时间:2018-12-18 23:29:32

标签: django-rest-framework

我建立了一个视图和序列化器,其中序列化器具有一个SerializerMethodField()字段。在该字段的方法中,我有类似以下内容:

def get_result(self, obj):
    result = Result(obj.a, obj.b, obj.c)
    return dict(
        percent_change=result.percent_change,
        [...]
    )

在某些记录中,我得到ContentNotRenderedError: The response content must be rendered before it can be accessed.

如果我将result.percent_change更改为固定值,例如1.1111,那么它会起作用。

get_result的内容包装在异常块中不会显示任何异常。

堆栈跟踪没有启发性,只有软件包代码:

Traceback (most recent call last):
  File "/opt/myapp/virtualenv-2.7.5/lib/python2.7/site-packages/django/contrib/staticfiles/handlers.py", line 63, in __call__
    return self.application(environ, start_response)
  File "/opt/myapp/virtualenv-2.7.5/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 157, in __call__
    response = self.get_response(request)
  File "/opt/myapp/virtualenv-2.7.5/lib/python2.7/site-packages/django/core/handlers/base.py", line 124, in get_response
    response = self._middleware_chain(request)
  File "/opt/myapp/virtualenv-2.7.5/lib/python2.7/site-packages/django/core/handlers/exception.py", line 43, in inner
    response = response_for_exception(request, exc)
  File "/opt/myapp/virtualenv-2.7.5/lib/python2.7/site-packages/django/core/handlers/exception.py", line 93, in response_for_exception
    response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
  File "/opt/myapp/virtualenv-2.7.5/lib/python2.7/site-packages/django/core/handlers/exception.py", line 139, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)
  File "/opt/myapp/virtualenv-2.7.5/lib/python2.7/site-packages/django_extensions/management/technical_response.py", line 6, in null_technical_500_response
    six.reraise(exc_type, exc_value, tb)
  File "/opt/myapp/virtualenv-2.7.5/lib/python2.7/site-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/opt/myapp/virtualenv-2.7.5/lib/python2.7/site-packages/django/core/handlers/base.py", line 249, in _legacy_get_response
    response = self._get_response(request)
  File "/opt/myapp/virtualenv-2.7.5/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/opt/myapp/virtualenv-2.7.5/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/opt/myapp/virtualenv-2.7.5/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
    return view_func(*args, **kwargs)
  File "/opt/myapp/virtualenv-2.7.5/lib/python2.7/site-packages/rest_framework/viewsets.py", line 103, in view
    return self.dispatch(request, *args, **kwargs)
  File "/opt/myapp/virtualenv-2.7.5/lib/python2.7/site-packages/rest_framework/views.py", line 485, in dispatch
    self.response = self.finalize_response(request, response, *args, **kwargs)
  File "/opt/myapp/virtualenv-2.7.5/lib/python2.7/site-packages/rest_framework_tracking/base_mixins.py", line 59, in finalize_response
    rendered_content = response.getvalue()
  File "/opt/myapp/virtualenv-2.7.5/lib/python2.7/site-packages/django/http/response.py", line 350, in getvalue
    return self.content
  File "/opt/myapp/virtualenv-2.7.5/lib/python2.7/site-packages/django/template/response.py", line 129, in content
    'The response content must be rendered before it can be accessed.'
ContentNotRenderedError: The response content must be rendered before it can be accessed.

1 个答案:

答案 0 :(得分:0)

percent_change有时是InfinityNaN。 JSON处理器无法处理此问题,并且正在DRF内部深处引发被吞咽的异常。

解决方案:用math.isinfmath.isnan检查值并将其更改为字符串,然后再从get_result()返回。