我正在使用 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"}