Django(JSONField)和tastypie [英] Django (JSONField) and tastypie
问题描述
这是我的模型的样子
从django.db导入模型
从json_field import JSONField
类模型(models.Model):
obj = JSONField()
我通过tastypie发送的值是
json_string ='{data:value}'
进入数据库我可以看到
code> {data:value}
但是当使用卷曲读取数据时我得到这样的东西
{u'data':u'value'}
我可以做什么,没有将python u'field的表示形式加入到tastypie的输出中?
谢谢!
我修正了这个问题:
def dehydrate_user_inputs(self,bundle):
requirements = Requirement.objects.get(pk = bundle.obj.pk)
user_inputs = json.dumps (requireme nt.user_inputs)
return user_inputs
我的JSONField命名为user_inputs。要求是它属于的模型。
当我的Tastypie已经这样做时,我觉得奇怪的是在做一个Query,但是这样做是有效的。如果有更好的解决方案,我会很喜欢。
I have a table into mysql that is the type TextField (django) by using the JSONField. This is how my model looks
from django.db import models
from json_field import JSONField
class Model(models.Model):
obj = JSONField()
The value I send via tastypie is
json_string = '{"data":"value"}'
Into the database I can see
{"data":"value"}
But when retrive the data with curl I get something like this
"{u'data': u'value'}"
What I can do to not have the python u'field' representation into the tastypie's output ?
thanks!
I fixed this issue like so:
def dehydrate_user_inputs(self, bundle):
requirement = Requirement.objects.get(pk = bundle.obj.pk)
user_inputs = json.dumps(requirement.user_inputs)
return user_inputs
My JSONField is named user_inputs. Requirement is the model that it belongs to.
I feel weird doing a Query here when Tastypie has already done so for me, but, this works. I'd love if there are better solutions.
这篇关于Django(JSONField)和tastypie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!