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

时间:2015-11-15 17:03:12

标签: python mongodb serialization embedded-resource eve

我有一个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的嵌入资源。但是,此资源包含component资源composition中密钥fluid的另一个嵌入资源。

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

我尝试做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"
}

1 个答案:

答案 0 :(得分:1)

我担心目前不支持。嵌入式资源序列化当前支持嵌套资源,但有一些limitations

  

目前,我们支持通过位于任何子文档(嵌套dicts和列表)中的引用嵌入文档。例如,查询/invoices?/embedded={"user.friends":1}将返回一个文档,其中包含用户和他的所有朋友,但仅当用户是子文档而朋友是参考列表时(它可能是一系列的dicts,嵌套的dict等。)

相关问题