所以我有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']
如果网址类似于https://domain.com/api/v1/question/,则应返回附加了属性响应的问题。虽然它们没有被序列化。
{
"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
}]
}
答案 0 :(得分:2)
您应该创建一个单独的private final void focusOnView(){
new Handler().post(new Runnable() {
@Override
public void run() {
your_scrollview.scrollTo(0, your_EditBox.getBottom());
}
});
}
并在 api.py 中进行链接。
ResponseResource
参数是API返回每个Response
full=True