Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>}
like image 825
gogasca Avatar asked Jan 27 '16 06:01

gogasca


1 Answers

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>')
like image 110
jumbopap Avatar answered Oct 12 '22 02:10

jumbopap