gpt4 book ai didi

python - 递归序列化 python-eve 查询中的所有嵌入资源

转载 作者:太空宇宙 更新时间:2023-11-03 17:15:08 24 4
gpt4 key购买 nike

我有一个Python-Eve - MongoDB 的 API,能够序列化嵌入式资源,如 docs 中所述。 .

在我的例子中,请求http://127.0.0.1:5000/sectors导致以下响应(不序列化嵌入式资源):

{
"_items": [
{
"mflow_fluid": 0.23,
"_id": "562692d055c40f709ce289d5",
"inlet_top": true,
"inlet_temp": 353,
"_etag": "53c3d9b10fc2bdcc4f68c7ed07d3ba13f57ca252",
"_created": "Tue, 20 Oct 2015 19:15:28 GMT",
"_updated": "Tue, 20 Oct 2015 19:15:28 GMT",
"name": "sector_heating",
"_links": {
"self": {
"title": "Sector",
"href": "sectors/562692d055c40f709ce289d5"
}
},
"angle_deg": 180,
"fluid": "562692d055c40f709ce289d4"
},
{
"mflow_fluid": 0.46,
"_id": "562692d055c40f709ce289d6",
"inlet_top": true,
"inlet_temp": 283,
"_etag": "0aaf153ff7417cde03bacb0601c5ee244d173cfe",
"_created": "Tue, 20 Oct 2015 19:15:28 GMT",
"_updated": "Tue, 20 Oct 2015 19:15:28 GMT",
"name": "sector_cooling",
"_links": {
"self": {
"title": "Sector",
"href": "sectors/562692d055c40f709ce289d6"
}
},
"angle_deg": 180,
"fluid": "562692d055c40f709ce289d4"
}
],
"_meta": {
"page": 1,
"max_results": 25,
"total": 2
},
"_links": {
"self": {
"title": "sectors",
"href": "sectors"
},
"parent": {
"title": "home",
"href": "/"
}
}
}

如您所见,键 fluid 包含嵌入式资源,可以使用 http://127.0.0.1:5000/sectors?embedded={"fluid 等请求序列化该资源":1},给出以下响应:

{
"_items": [
{
"mflow_fluid": 0.23,
"_id": "562692d055c40f709ce289d5",
"inlet_top": true,
"inlet_temp": 353,
"_etag": "53c3d9b10fc2bdcc4f68c7ed07d3ba13f57ca252",
"_created": "Tue, 20 Oct 2015 19:15:28 GMT",
"_updated": "Tue, 20 Oct 2015 19:15:28 GMT",
"name": "sector_heating",
"_links": {
"self": {
"title": "Sector",
"href": "sectors/562692d055c40f709ce289d5"
}
},
"angle_deg": 180,
"fluid": {
"specific_heat": 1005,
"_id": "562692d055c40f709ce289d4",
"specific_gas_constant": 287.12,
"_etag": "7c9c9c1d5e5dfe5414068d0a12736a1721d05926",
"name": "air",
"_updated": "Tue, 20 Oct 2015 19:15:28 GMT",
"composition": [
{
"fraction": 0.79,
"component": "562692cf55c40f709ce289d2"
},
{
"fraction": 0.21,
"component": "562692d055c40f709ce289d3"
}
],
"state": "gaseous",
"_created": "Tue, 20 Oct 2015 19:15:28 GMT"
}
},
{
"mflow_fluid": 0.46,
"_id": "562692d055c40f709ce289d6",
"inlet_top": true,
"inlet_temp": 283,
"_etag": "0aaf153ff7417cde03bacb0601c5ee244d173cfe",
"_created": "Tue, 20 Oct 2015 19:15:28 GMT",
"_updated": "Tue, 20 Oct 2015 19:15:28 GMT",
"name": "sector_cooling",
"_links": {
"self": {
"title": "Sector",
"href": "sectors/562692d055c40f709ce289d6"
}
},
"angle_deg": 180,
"fluid": {
"specific_heat": 1005,
"_id": "562692d055c40f709ce289d4",
"specific_gas_constant": 287.12,
"_etag": "7c9c9c1d5e5dfe5414068d0a12736a1721d05926",
"name": "air",
"_updated": "Tue, 20 Oct 2015 19:15:28 GMT",
"composition": [
{
"fraction": 0.79,
"component": "562692cf55c40f709ce289d2"
},
{
"fraction": 0.21,
"component": "562692d055c40f709ce289d3"
}
],
"state": "gaseous",
"_created": "Tue, 20 Oct 2015 19:15:28 GMT"
}
}
],
"_meta": {
"page": 1,
"max_results": 25,
"total": 2
},
"_links": {
"self": {
"title": "sectors",
"href": "sectors"
},
"parent": {
"title": "home",
"href": "/"
}
}
}

关键fluid的嵌入资源根据需要进行序列化。但是,此资源包含 fluidcomposition 资源中关键 component 的另一个嵌入资源。

是否有一种方法可以“递归”序列化所有嵌入资源,以获得完全序列化的资源作为响应?

我尝试执行类似 http://127.0.0.1:5000/sectors?embedded={"fluid":1 "fluid.composition.component":1} 的操作,结果为 400 -响应:

{
"_error": {
"code": 400,
"message": "Unable to parse `embedded` clause"
},
"_status": "ERR"
}

最佳答案

恐怕目前不支持这一点。嵌入式资源序列化目前支持嵌套资源,但有一些limitations :

Currently we support embedding of documents by references located in any subdocuments (nested dicts and lists). For example, a query /invoices?/embedded={"user.friends":1} will return a document with user and all his friends embedded, but only if user is a subdocument and friends is a list of reference (it could be a list of dicts, nested dict, etc.)

关于python - 递归序列化 python-eve 查询中的所有嵌入资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33722344/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com