Python Eve contains filter

天涯浪子 提交于 2019-12-22 08:08:41

问题


There's some way to return items that field contains some value? Eg.

GET /people?contains="foo"

Return all persons that have the word 'foo' in the name.

Thanks in advance


回答1:


You could use mongodb $regex operator, which is blacklisted by default in Eve (MONGO_QUERY_BLACKLIST = ['$where', '$regex']).

Add MONGO_QUERY_BLACKLIST = ['$where'] to your settings.py. Then you can query your API like this:

?where={"name": {"$regex": ".*foo.*"}}.

Be careful however. If you don't control the client, enabling regexes could potentially increase your API vulnerability.




回答2:


I am not conversant with Eve myself. But looking at it's webpage seems like it exposes all of Flask's functionalities.

You need to be looking at this page in the documentation that talks about accessing the request data.

In your Flask app, define a method that accepts both POST and GET requests and then you can access foo by doing request.args.get('contains', '').

This is what I mean:

@app.route('/people', methods=['POST', 'GET'])
def get_people():
    search_key = request.args.get('contains', '')
    #search for people containing 'foo' in your DB

Hope this gives you a starting point as to how to go about things.



来源:https://stackoverflow.com/questions/23097154/python-eve-contains-filter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!