Modify JSON response of Flask-Restless Modify JSON response of Flask-Restless flask flask

Modify JSON response of Flask-Restless


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 resultsmanager.create_api(    ...,    postprocessors={        'GET_MANY': [pagination_remover]    })

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


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]