来自django rest框架的自定义和嵌套响应

时间:2015-09-22 08:10:40

标签: python django rest

我有这些模型用于django和django-rest框架。

[{"id" : 1, "name": "Type A"}, 
 {"id" : 2, "name": "Type B"}]

模型对象保存为

产品类型

[{"id" : 1, "name": "Product A", "type": 1}, 
 {"id" : 2, "name": "Product B", "type": 2}]

平台

[{"id" : 1, "name": "Category A"}, 
 {"id" : 2, "name": "Category B"},
 {"id" : 3, "name": "Category C"}]

分类

[{"id" : 1, "name": "Attribute A", "category":1, "type": 1}, 
 {"id" : 2, "name": "Attribute B", "category":1, "type": 2}, 
 {"id" : 3, "name": "Attribute C", "category":1, "type": 1}, 
 {"id" : 4, "name": "Attribute D", "category":1, "type": 1}, 
 {"id" : 5, "name": "Attribute E", "category":2, "type": 1}, 
 {"id" : 6, "name": "Attribute F", "category":2, "type": 1}, 
 {"id" : 7, "name": "Attribute G", "category":2, "type": 2}, 
 {"id" : 8, "name": "Attribute H", "category":3, "type": 2}, 
 {"id" : 9, "name": "Attribute I", "category":3, "type": 2}]

属性

{
    "id": 1,
    "name": "Product A",
    "type": 1,
    "categories": [
        {
            "id": 1,
            "name": "Category A",
            "attributes": [
                {
                    "id": 1,
                    "name": "Attribute A"
                },
                {
                    "id": 2,
                    "name": "Attribute C"
                },
                {
                    "id": 4,
                    "name": "Attribute D"
                }
            ]
        },
        {
            "id": 2,
            "name": "Category B",
            "attributes": [
                {
                    "id": 5,
                    "name": "Attribute E"
                },
                {
                    "id": 6,
                    "name": "Attribute F"
                }
            ]
        }
    ]
}

我希望得到一个PK = 1的Product对象,如下所述

then()

目前,我正在查询每个模型,然后根据需要在客户端重新组织结果。

但是,我想在服务器端执行相同的任务,以使客户端代码变得简单。

注意,平台和类别没有直接关系。

0 个答案:

没有答案