[Django]-Tastypie: JSON header to use UTF-8

11👍

This is because they patched Tastypie to no longer send charset=utf-8 when content-type is application/json or text/javascript per https://github.com/toastdriven/django-tastypie/issues/717.

If you look into tastypie/utils/mime.py you will notice the following lines:

def build_content_type(format, encoding='utf-8'):
    """
    Appends character encoding to the provided format if not already present.
    """
    if 'charset' in format:
        return format

    if format in ('application/json', 'text/javascript'):
        return format

    return "%s; charset=%s" % (format, encoding)

You can either remove the two lines

if format in ('application/json', 'text/javascript'):
    return format

or if you don’t want to modify the Tastypie source code, do what I did.

I noticed that build_content_type is used in create_response method of ModelResource, so I created a new ModelResource as a subclass of ModelResource and overrode the method.

from django.http import HttpResponse
from tastypie import resources

def build_content_type(format, encoding='utf-8'):
    """
    Appends character encoding to the provided format if not already present.
    """
    if 'charset' in format:
        return format

    return "%s; charset=%s" % (format, encoding)

class MyModelResource(resources.ModelResource):
    def create_response(self, request, data, response_class=HttpResponse, **response_kwargs):
        """
        Extracts the common "which-format/serialize/return-response" cycle.

        Mostly a useful shortcut/hook.
        """
        desired_format = self.determine_format(request)
        serialized = self.serialize(request, data, desired_format)
        return response_class(content=serialized, content_type=build_content_type(desired_format), **response_kwargs)

Then, I changed my resources to inherit from this class instead.

class MyResource(MyModelResource):
    class Meta:
        queryset = MyObject.objects.all()
👤se210

Leave a comment