我正面临与here所述的完全相同的问题。我的API有user_creds
个端点。当我访问localhost:5000/user_creds/
时,可以看到该集合中的所有文档。但是,当我执行localhost:5000/user_creds/[email protected]
之类的操作时,总是收到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==%[email protected]%22
,则将获得集合中的所有文档,而不是仅获得与电子邮件正则表达式匹配的单个文档。如果我访问此http://127.0.0.1:5000/user_creds?where=email==%[email protected]%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/[email protected]
localhost:5000/user_creds/someemail%40gmail.com
如果您有兴趣通过电子邮件only(不是对象ID)在商品级别检索商品,则可以将item_lookup_field与item_url一起使用:
'user_creds': {
...
'item_url': 'regex("[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$")',
'item_lookup_field': 'email'
}