UnicodeDecodeError after upgrading to django-rest-framework 3

On django-rest-framework 2 it works fine:

from rest_framework import rest_response, generics from rest_framework.renderers import JSONRenderer, BrowsableAPIRenderer class SomeView(generics.GenericAPIView): renderer_classes = JSONRenderer, BrowsableAPIRenderer def get(self, request, *args, **kwargs): ... # Build a response dict with non-ascii in it ... return rest_response.Response(some_dict_with_non_ascii_in_it_somewhere) 

I do not need to explicitly encode any non-ascii ...

However, after upgrading to DRF 3, the same code now throws the following error:

 Traceback (most recent call last): File "/Users/troy/.virtualenvs/publisher/lib/python2.7/site-packages/django/contrib/staticfiles/handlers.py", line 63, in __call__ return self.application(environ, start_response) File "/Users/troy/.virtualenvs/publisher/lib/python2.7/site-packages/whitenoise/base.py", line 119, in __call__ return self.application(environ, start_response) File "/Users/troy/.virtualenvs/publisher/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 189, in __call__ response = self.get_response(request) File "/Users/troy/.virtualenvs/publisher/lib/python2.7/site-packages/django/core/handlers/base.py", line 218, in get_response response = self.handle_uncaught_exception(request, resolver, sys.exc_info()) File "/Users/troy/.virtualenvs/publisher/lib/python2.7/site-packages/django/core/handlers/base.py", line 261, in handle_uncaught_exception return debug.technical_500_response(request, *exc_info) File "/Users/troy/.virtualenvs/publisher/lib/python2.7/site-packages/django_extensions/management/technical_response.py", line 5, in null_technical_500_response six.reraise(exc_type, exc_value, tb) File "/Users/troy/.virtualenvs/publisher/lib/python2.7/site-packages/django/core/handlers/base.py", line 164, in get_response response = response.render() File "/Users/troy/.virtualenvs/publisher/lib/python2.7/site-packages/django/template/response.py", line 158, in render self.content = self.rendered_content File "/Users/troy/.virtualenvs/publisher/lib/python2.7/site-packages/rest_framework/response.py", line 71, in rendered_content ret = renderer.render(self.data, media_type, context) File "/Users/troy/.virtualenvs/publisher/lib/python2.7/site-packages/rest_framework/renderers.py", line 104, in render separators=separators File "/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 250, in dumps sort_keys=sort_keys, **kw).encode(obj) File "/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 210, in encode return ''.join(chunks) UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 671: ordinal not in range(128) 

I assume that DRF 3 now has some new configuration value, which was the default in DRF 2. I tried to set the REST_FRAMEWORK UNICODE_JSON parameter to True , but I still get the same error ..

Is there a parameter that can make this figure behave like DRF 2? or DRF 3 do I need to track down the non-ascii character in my dictionary and manually encode it?

+6
source share
2 answers

I have found the answer.

In DRF 2, rest_framework.JSONRenderer.ensure_ascii set to True . In DRF 3, rest_framework.JSONRenderer.ensure_ascii set to not api_settings.UNICODE_JSON (I missed not before when I wrote the question ...).

So, to make it behave like DRF 2, I had to set "UNICODE_JSON" to False instead of True , as before (by default it is True):

 REST_FRAMEWORK = { ... 'UNICODE_JSON': False } 

As an alternative, I could, of course, encode the dictionary values, which in many cases might be the best option.

+9
source

By default, Python 2.7 considers strings to be binary. Try adding to the top of your files:

 from __future__ import unicode_literals 

This will make your unicode strings the default and should help them correctly convert them.

+2
source

All Articles