Search code examples
pythonflaskflask-sqlalchemyflask-restful

Flask Restful search query


I have migrated my Flask API from Restless to Restful. Flask search query not using filters

Is there a way to perform search queries from client similar to Flask Restless? http://flask-restless.readthedocs.org/en/latest/searchformat.html

They use this format in the curl request:

{"name": <fieldname>, "op": <operatorname>, "val": <argument>}

Solution

  • Flask-Restful doesn't magic away your API, since it doesn't have any built-in connection to your database. You'll have to write the logic yourself. Here's one possible way to do it.

    class UserSearch(Resource):
        def get(self, search_term):
            results = User.query.filter(User.name.like('%'+search_term+'%')).all()
    
            # serialize and return items...
    
    api.add_resource(UserSearch, '/users/search/<search_term>')