JSONfieldを使用して、JSON形式のデータを1つのフィールドに格納しています。しかし、tasty-pieを使用してビルドするAPIにアクセスすると、期待どおりにネストされた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"}