django-tasty-pie将char字段转换为JSON

时间:2013-03-13 17:27:50

标签: django api rest tastypie

我正在使用JSONfield将一些JSON格式化数据存储在一个字段中。但是,当我转到我的API,它是在Tasty-pie的帮助下构建的,它将jason作为字符串返回,而不是像我预期的那样嵌套JSON。

models.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"}

1 个答案:

答案 0 :(得分:4)

创建捆绑后,response is createdserializing the content。对于json格式和默认的Serializer,它首先是convert any complex type to python basic types,然后是dumps it in a string representation

正如您所看到的,在原始字符串的情况下,它只是将其转换为unicode。在序列化资源之前,您需要将其转换为dict。因此,您可以使用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表示的语句。