TastyPie 在脱水过程中序列化
TastyPie Serializing during dehydrate
所以我有一个 QuestionResource
:
class QuestionResourse(ModelResource):
def dehydrate(self, bundle):
bundle.data['responses'] = Responses.objects.filter(question_id=bundle.data['id'])
return bundle
class Meta:
resource_name='question'
queryset = Questions.objects.all()
allowed_methods = ['get', 'post']
如果 url 类似于 https://domain.com/api/v1/question/,它应该 return 附有属性响应的问题。虽然他们没有被连载。
{
"date": "2015-10-03T16:53:22",
"id": "1",
"question": "Where is my mind?",
"resource_uri": "/api/v1/question/1/",
"responses": "[<Responses: Responses object>, <Responses: Responses object>, <Responses: Responses object>, <Responses: Responses object>, <Responses: Responses object>]",
"totalresponses": 5
}
如何序列化 <Responses: Responses object>
?
另外,如何将 "responses"
变成 json 数组而不是字符串?
编辑:
在 raphv 的帮助下,我在我的资源中使用了这段代码:
class ResponseResourse(ModelResource):
class Meta:
resource_name='response'
queryset = Responses.objects.all()
allowed_methods = ['get', 'post']
class QuestionResourse(ModelResource):
responses = fields.ToManyField(ResponseResourse, attribute=lambda bundle: Responses.objects.filter(question_id = bundle.obj.id), full=True)
class Meta:
resource_name='question'
queryset = Questions.objects.all()
allowed_methods = ['get', 'post']
生产:
{
"date": "2015-10-03T16:53:22",
"id": "1",
"question": "Where is my mind?",
"resource_uri": "/api/v1/question/1/",
"responses": [
{
"id": "54",
"resource_uri": "/api/v1/response/54/",
"response": "ooooooo oooooo",
},
{
"id": "60",
"resource_uri": "/api/v1/response/60/",
"response": "uhh, test",
"votes": 0
}]
}
您应该在 api.py.
中创建一个单独的 ResponseResource
和 link
full=True
参数使 API return 成为每个响应的完整表示
from tastypie import resources, fields
class ResponseResource(resources.ModelResource):
class Meta:
resource_name = 'response'
queryset = Responses.objects.all()
...
class QuestionResource(resources.ModelResource):
responses = fields.ToManyField(ResponseResource, 'responses', full=True)
class Meta:
resource_name='question'
queryset = Questions.objects.all()
...
所以我有一个 QuestionResource
:
class QuestionResourse(ModelResource):
def dehydrate(self, bundle):
bundle.data['responses'] = Responses.objects.filter(question_id=bundle.data['id'])
return bundle
class Meta:
resource_name='question'
queryset = Questions.objects.all()
allowed_methods = ['get', 'post']
如果 url 类似于 https://domain.com/api/v1/question/,它应该 return 附有属性响应的问题。虽然他们没有被连载。
{
"date": "2015-10-03T16:53:22",
"id": "1",
"question": "Where is my mind?",
"resource_uri": "/api/v1/question/1/",
"responses": "[<Responses: Responses object>, <Responses: Responses object>, <Responses: Responses object>, <Responses: Responses object>, <Responses: Responses object>]",
"totalresponses": 5
}
如何序列化 <Responses: Responses object>
?
另外,如何将 "responses"
变成 json 数组而不是字符串?
编辑: 在 raphv 的帮助下,我在我的资源中使用了这段代码:
class ResponseResourse(ModelResource):
class Meta:
resource_name='response'
queryset = Responses.objects.all()
allowed_methods = ['get', 'post']
class QuestionResourse(ModelResource):
responses = fields.ToManyField(ResponseResourse, attribute=lambda bundle: Responses.objects.filter(question_id = bundle.obj.id), full=True)
class Meta:
resource_name='question'
queryset = Questions.objects.all()
allowed_methods = ['get', 'post']
生产:
{
"date": "2015-10-03T16:53:22",
"id": "1",
"question": "Where is my mind?",
"resource_uri": "/api/v1/question/1/",
"responses": [
{
"id": "54",
"resource_uri": "/api/v1/response/54/",
"response": "ooooooo oooooo",
},
{
"id": "60",
"resource_uri": "/api/v1/response/60/",
"response": "uhh, test",
"votes": 0
}]
}
您应该在 api.py.
中创建一个单独的ResponseResource
和 link
full=True
参数使 API return 成为每个响应的完整表示
from tastypie import resources, fields
class ResponseResource(resources.ModelResource):
class Meta:
resource_name = 'response'
queryset = Responses.objects.all()
...
class QuestionResource(resources.ModelResource):
responses = fields.ToManyField(ResponseResource, 'responses', full=True)
class Meta:
resource_name='question'
queryset = Questions.objects.all()
...