I am trying to use Flask-Restless with Ember.js which isn't going so great. It's the GET responses that are tripping me up. For instance, when I do a GET request on /api/people for example Ember.js expects:

{ 
    people: [
        { id: 1, name: "Yehuda Katz" }
    ] 
}

But Flask-Restless responds with:

{
    "total_pages": 1, 
    "objects": [
        { "id": 1, "name": "Yahuda Katz" }
    ], 
    "num_results": 1, 
    "page": 1
}

How do I change Flask-Restless's response to conform to what Ember.js would like? I have this feeling it might be in a postprocessor function, but I'm not sure how to implement it.

有帮助吗?

解决方案 2

The accepted answer was correct at the time. However the post and preprocessors work in Flask-Restless have changed. According to the documentation:

The preprocessors and postprocessors for each type of request accept different arguments, but none of them has a return value (more specifically, any returned value is ignored). Preprocessors and postprocessors modify their arguments in-place.

So now in my postprocessor I just delete any keys that I do not want. For example:

def api_post_get_many(result=None, **kw):
    for key in result.keys():
        if key != 'objects':
            del result[key]

其他提示

Flask extensions have pretty readable source code. You can make a GET_MANY postprocessor:

def pagination_remover(results):
    return {'people': results['objects']} if 'page' in results else results

manager.create_api(
    ...,
    postprocessors={
        'GET_MANY': [pagination_remover]
    }
)

I haven't tested it, but it should work.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top