递归地序列化pythoneve查询中的所有嵌入资源

2024-10-02 12:38:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个指向MongoDB的Python-Eve-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}之类的请求对其进行序列化,并给出以下响应:

^{pr2}$

fluid的嵌入资源按需序列化。但是,此资源包含另一个嵌入资源,该资源位于fluid的{}资源中的键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"
}

Tags: selfhttp序列化titlelinks资源octembedded
1条回答
网友
1楼 · 发布于 2024-10-02 12:38:19

恐怕目前还不支持。嵌入式资源序列化当前支持嵌套资源,但有一些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.)

相关问题 更多 >

    热门问题