Python-Eve:不止一次额外查找
Python-Eve: More than one additional lookup
使用 additional lookups 我能够通过辅助文档访问所需端点的给定文档,如文档中所述:
Besides the standard item endpoint which defaults to
/<resource>/<ID_FIELD_value>
, you can optionally define a secondary,
read-only, endpoint like /<resource>/<person_name>
. You do so by
defining a dictionary comprised of two items field and url. The former
is the name of the field used for the lookup. If the field type (as
defined in the resource schema) is a string, then you put a URL rule
in url. If it is an integer, then you just omit url, as it is
automatically handled. See the code snippet below for an usage example
of this feature.
因此回顾文档中的给定示例:
people = {
# 'title' tag used in item links. Defaults to the resource title minus
# the final, plural 's' (works fine in most cases but not for 'people')
'item_title': 'person',
# by default, the standard item entry point is defined as
# '/people/<ObjectId>/'. We leave it untouched, and we also enable an
# additional read-only entry point. This way consumers can also perform
# GET requests at '/people/<lastname>'.
'additional_lookup': {
'url': 'regex("[\w]+")',
'field': 'lastname'
},
# We choose to override global cache-control directives for this resource.
'cache_control': 'max-age=10,must-revalidate',
'cache_expires': 10,
# we only allow GET and POST at this resource endpoint.
'resource_methods': ['GET', 'POST'],
}
由于 lastname
被设置为次要端点,我可以通过文档 ID(默认)或 lastname
键访问 people
端点存储的文档。
假设每个人都有一个独一无二的lastname
和一个独一无二的nickname
。是否可以定义多个附加查找以便通过 lastname
和 nickname
进行访问?
然而,这个例子只是为了展示我正在寻找的东西。在我的真实用例中,我有一个包含不同产品信息的数据库,我希望能够通过英文和德文标题访问这些信息,这样我就可以保证我所有的额外查找都会产生唯一的文档密钥。
您不能向同一个端点添加多个额外的查找。但是,您可以做的是让多个端点使用相同的数据源。
Multiple API endpoints can target the same database collection. For example you can set both /admins
and /users
to read and write from the same people collection on the database.
引用自Advanced Datasource Patterns。所以你可以简单地使用 /books/english/<title>
和 /books/german/<title>
。两个端点仍将使用相同的数据库集合。
根据您的模式设计,您也可以使用 Sub Resources 路径并设置类似 /products/<language>/books/<title>
.
的内容
使用 additional lookups 我能够通过辅助文档访问所需端点的给定文档,如文档中所述:
Besides the standard item endpoint which defaults to
/<resource>/<ID_FIELD_value>
, you can optionally define a secondary, read-only, endpoint like/<resource>/<person_name>
. You do so by defining a dictionary comprised of two items field and url. The former is the name of the field used for the lookup. If the field type (as defined in the resource schema) is a string, then you put a URL rule in url. If it is an integer, then you just omit url, as it is automatically handled. See the code snippet below for an usage example of this feature.
因此回顾文档中的给定示例:
people = {
# 'title' tag used in item links. Defaults to the resource title minus
# the final, plural 's' (works fine in most cases but not for 'people')
'item_title': 'person',
# by default, the standard item entry point is defined as
# '/people/<ObjectId>/'. We leave it untouched, and we also enable an
# additional read-only entry point. This way consumers can also perform
# GET requests at '/people/<lastname>'.
'additional_lookup': {
'url': 'regex("[\w]+")',
'field': 'lastname'
},
# We choose to override global cache-control directives for this resource.
'cache_control': 'max-age=10,must-revalidate',
'cache_expires': 10,
# we only allow GET and POST at this resource endpoint.
'resource_methods': ['GET', 'POST'],
}
由于 lastname
被设置为次要端点,我可以通过文档 ID(默认)或 lastname
键访问 people
端点存储的文档。
假设每个人都有一个独一无二的lastname
和一个独一无二的nickname
。是否可以定义多个附加查找以便通过 lastname
和 nickname
进行访问?
然而,这个例子只是为了展示我正在寻找的东西。在我的真实用例中,我有一个包含不同产品信息的数据库,我希望能够通过英文和德文标题访问这些信息,这样我就可以保证我所有的额外查找都会产生唯一的文档密钥。
您不能向同一个端点添加多个额外的查找。但是,您可以做的是让多个端点使用相同的数据源。
Multiple API endpoints can target the same database collection. For example you can set both
/admins
and/users
to read and write from the same people collection on the database.
引用自Advanced Datasource Patterns。所以你可以简单地使用 /books/english/<title>
和 /books/german/<title>
。两个端点仍将使用相同的数据库集合。
根据您的模式设计,您也可以使用 Sub Resources 路径并设置类似 /products/<language>/books/<title>
.