Python Eve - REST API additional_lookup not working

a 夏天 提交于 2019-12-10 19:47:20

问题


I am facing the exact same issue that is described here. I have user_creds endpoint for my API. When I visit localhost:5000/user_creds/, I can see all the documents in that collection. But when I do something like localhost:5000/user_creds/someemail@gmail.com, I always get a 404 Not Found response.

user_creds domain in settings.py looks like this:

'user_creds': {
        'schema': { 
            'email': {
                'type': 'string',
                'regex': r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)",
                'required': True,
                'unique': True,
            },
            'password': {
                'type': 'string',
                'required': True
            },
        }

        'resource_methods': ['GET', 'POST'],

        'item_methods': ['GET', 'PATCH', 'PUT'],

        'additional_lookup': {
            'url': 'regex("^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$")',
            'field': 'email'
        }
    }

I am following the example given here but cant figure out where I am going wrong. Also, if I visit this URL: http://127.0.0.1:5000/user_creds?email==%22abcd12@gmail.com%22, I get all the documents in the collection instead of getting just a single document which matches the email regex. If I visit this http://127.0.0.1:5000/user_creds?where=email==%22abcd12@gmail.com%22, I am getting the desired response.


回答1:


@Vorticity has the right fix. Just remove the leading "^" in your additional_lookup regex as follows:

'additional_lookup': {
    'url': 'regex("[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$")',
    'field': 'email'
}

You should be able to retrieve your item with or without url encoding eg:

localhost:5000/user_creds/someemail@gmail.com
localhost:5000/user_creds/someemail%40gmail.com

If you have any interest in making your items retrievable at the item level by email only (not object id), you can use item_lookup_field together with item_url:

'user_creds': {
    ...
    'item_url': 'regex("[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$")',
    'item_lookup_field': 'email'
}


来源:https://stackoverflow.com/questions/52306826/python-eve-rest-api-additional-lookup-not-working

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