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

Serialize all embedded resources in python-eve query recursively

我有一个 Python-Eve-API to a MongoDB which is able to serialize embedded resources as described in the 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 的另一个嵌入资源。

有没有办法序列化所有嵌入的资源'recursively'以获得完全序列化的资源作为响应?

我试图做类似 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.)