我正面临与here所述的完全相同的问题。我的API有user_creds终结点。当我访问localhost:5000/user_creds/时,可以看到该集合中的所有文档。但是,当我执行类似localhost:5000/user_creds/someemail@gmail.com的操作时,总是收到404 Not Found响应。

settings.py中的user_creds域如下所示:

'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'
        }
    }


我正在按照给出here的示例进行操作,但无法弄清楚我要去哪里。另外,如果我访问以下URL:http://127.0.0.1:5000/user_creds?email==%22abcd12@gmail.com%22,我将获得集合中的所有文档,而不是仅获得一个与电子邮件正则表达式匹配的文档。如果我访问此http://127.0.0.1:5000/user_creds?where=email==%22abcd12@gmail.com%22,则会得到所需的响应。

最佳答案

@Vorticity具有正确的解决方案。只需按如下所示删除您的Additional_lookup正则表达式中的前导“ ^”即可:

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


您应该能够使用或不使用网址编码来检索您的商品,例如:

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


如果您有兴趣通过邮件(而不是对象ID)在商品级别检索商品,可以将item_lookup_fielditem_url一起使用:

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

关于python - Python前夕-REST API Additional_lookup无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52306826/

10-15 22:10