1

我正在使用 JSONfield 在一个字段中存储一些 JSON 格式的数据。但是,当我转到我的 API 时,它在美味派的帮助下构建,它以字符串的形式返回 jason,而不是我预期的嵌套 JSON。

模型.py

class Item(models.Model):
    user = models.ForeignKey(User)
    body = JSONField(max_length=1024)

api.py

 class ItemResource(ModelResource):
    authorization = Authorization()
    authentication = SessionAuthentication()
    list_allowed_methods = ['get', 'post']

    class Meta:
        queryset = Item.objects.filter()

    def get_object_list(self, request):
        return super(ItemResource, self).get_object_list(request).filter(user=request.user)

    def apply_authorization_limits(self, request, object_list):
        return object_list.filter(user=request.user)

    def dehydrate(self, bundle):
        # how to modify bundle['body'] here ? so it work
        return bundle

显然 JSONfield 是从 django 的标准 TextField 继承而来的。因此,当我检索 API 时,我收到以下信息:

 {
    "meta": {
        "limit": 20,
        "next": null,
        "offset": 0,
        "previous": null,
        "total_count": 1
    },
    "objects": [
       {
        "body": "{u'valid': u'json'}",
        "id": 1,
        "resource_uri": "/api/v1/testitem/1/"
        }
    ]
  }

这就是我想要收到的:

 {
    "meta": {
        "limit": 20,
        "next": null,
        "offset": 0,
        "previous": null,
        "total_count": 1
    },
    "objects": [
       {
        "body": {
             "valid': "json"
           },
        "id": 1,
        "resource_uri": "/api/v1/testitem/1/"
        }
    ]
  }

注意body领域的差异?

如果我这样做:

    def dehydrate(self, bundle):
        bundle['body'] = json.loads(bundle['body'])
        return bundle

我收到此异常:

{"error_message": "Expecting property name: line 1 column 1 (char 1)", "traceback": "Traceback (most recent call last):\n\n  File \"/Users/polinom/Envs/django/lib/python2.7/site-packages/tastypie/resources.py\", line 202, in wrapper\n    response = callback(request, *args, **kwargs)\n\n  File \"/Users/polinom/Envs/django/lib/python2.7/site-packages/tastypie/resources.py\", line 439, in dispatch_list\n    return self.dispatch('list', request, **kwargs)\n\n  File \"/Users/polinom/Envs/django/lib/python2.7/site-packages/tastypie/resources.py\", line 471, in dispatch\n    response = method(request, **kwargs)\n\n  File \"/Users/polinom/Envs/django/lib/python2.7/site-packages/tastypie/resources.py\", line 1270, in get_list\n    bundles.append(self.full_dehydrate(bundle))\n\n  File \"/Users/polinom/Envs/django/lib/python2.7/site-packages/tastypie/resources.py\", line 845, in full_dehydrate\n    bundle = self.dehydrate(bundle)\n\n  File \"/Users/polinom/workspace/microjob/applications/examinations/api.py\", line 24, in dehydrate\n    bundle.data['body'] = json.loads(bundle.data['body'])\n\n  File \"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py\", line 326, in loads\n    return _default_decoder.decode(s)\n\n  File \"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py\", line 360, in decode\n    obj, end = self.raw_decode(s, idx=_w(s, 0).end())\n\n  File \"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py\", line 376, in raw_decode\n    obj, end = self.scan_once(s, idx)\n\nValueError: Expecting property name: line 1 column 1 (char 1)\n"}
4

1 回答 1

4

创建捆绑包后,通过序列化内容来创建响应。对于 json 格式和默认的 Serializer,它首先将任何复杂类型转换为 python 基本类型,然后将其转储为字符串表示形式

如您所见,对于原始字符串,它只是将其转换为 unicode。您需要dict在序列化资源之前将其转换为 a 。因此,在dehydrate您可以执行的方法中:

def dehydrate(self, bundle):
    bundle['body'] = json.loads(bundle.data['body'])
    return bundle

由于您只修改一个元素,您还可以执行以下操作:

def dehydrate_body(self, bundle):
    return json.loads(bundle.data['body'])

JSONField 存储 python dict 表示,而不是 JSON 表示。所以,我的第一次尝试是不正确的。你可以return eval(bundle.data['body'])。IMO 在eval这里使用是安全的,因为 JSONField 确保内容是可以直接转换为 json 表示的语句。

于 2013-03-13T18:26:01.997 回答