Я построил представление и сериализатор, где в сериализаторе есть поле, равное 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.