Python Eve contains filter

2019-05-11 02:57发布

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

2条回答
Melony?
2楼-- · 2019-05-11 03:34

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.

查看更多
干净又极端
3楼-- · 2019-05-11 03:38

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.

查看更多
登录 后发表回答