如何在不使用 Django REST Framework 转义的情况下将 TextField 作为文字 JSON 提供?

How to serve TextField as literal JSON without escaping with Django REST Framework?

我们正在通过 TextField 将原始 JSON 数据存储在我们的模型中,并希望该数据由 REST Framework API。

内容正在转义并呈现为字符串而不是对象的一部分。例如{\r\n \"phases\": [\r\n \"S

来自 requirements.txt

    Django==1.8.2
    djangorestframework==3.1.3

将以下内容添加到我的序列化程序(以及 urls.py 顶部的 import json

def to_representation(self, instance):
  ret = super(RoadmapSerializer, self).to_representation(instance)
  ret['jsonField'] = json.loads(ret['jsonField'])
  return ret

nb: jsonField 是在 models.py

中声明的模型属性的名称
jsonField = models.TextField(verbose_name="JSON", blank=True)

灵感来源 - django-rest-framework: How Do I Serialize a Field That Already Contains JSON?(Almalki 和 Denis Cornehl 的回答) - http://www.django-rest-framework.org/topics/3.0-announcement/#serializers(因为 transform_ 在 drf 3.0 中被弃用)