這是因爲他們修補Tastypie不再發送charset = utf-8時,content-類型是應用程序/ json或文本/ JavaScript每https://github.com/toastdriven/django-tastypie/issues/717。
如果你看看tastypie/utils的/ mime.py你會發現下面幾行:
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)
您可以刪除這兩條線
if format in ('application/json', 'text/javascript'):
return format
,或者如果你不想修改Tastypie源代碼,做我所做的。
我注意到在ModelResource的create_response方法中使用了build_content_type,所以我創建了一個新的ModelResource作爲ModelResource的子類並覆蓋了該方法。
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)
然後,我改變了我的資源,而不是從這個類繼承。
class MyResource(MyModelResource):
class Meta:
queryset = MyObject.objects.all()
不太明白你的問題。有什麼問題?你能發表一些'json'的例子嗎?或代碼? –