在Eve中,如何创建集合的子资源并保留父集合端点?

时间:2014-10-02 17:37:13

标签: python eve

我想要这三个端点:

/games
/images
/games/<game_id>/images

以下是我的settings.py文件摘录

#...

games = {
    "schema": {
        "title": {
            "type": "string",
            "required": True
        },
        "name": {
            "type": "string",
            "required": True
        },
    }
}

images = {
    "schema": {
        "game_id": {
            "type": "string",
            "required": True,
        },
        "title": {
            "type": "string",
            "required": True,
        },
    },
    "url": "games/<regex('[a-f0-9]{24}'):game_id>/images"
}
#...

如果省略url属性,则在获取GET时会得到两个预期的端点:

/games

/images

但是如果你包含了url属性,则无法点击/ images,而只能点击/ games和/games/<game_id>/images,如下所示:

{
    "_links": {
        "child": [
            {
                "href": "/games/<regex('[a-f0-9]{24}'):game_id>/images",
                "title": "games/<regex('[a-f0-9]{24}'):game_id>/images"
            },
            {
                "href": "/games",
                "title": "games"
            }
        ]
    }
}

如何保留集合图像并仍然通过子资源查询使其文档可用?

1 个答案:

答案 0 :(得分:3)

您可以设置3个不同的端点,而其中两个端点消耗相同的数据库资源(图像)。像这样:

images_schema: {
  "game_id": {
    "type": "string",
    "required": True,
  },
  "title": {
    "type": "string",
    "required": True,
  },
}

games = {
  "schema": {
    "title": {
      "type": "string",
      "required": True
    },
    "name": {
      "type": "string",
      "required": True
    },
  }
}

images = {
  "schema": images_schema,
  "url": "images"  # not really needed
}

games_images = {
  "schema": images_schema,
  "url": "games/<regex('[a-f0-9]{24}'):game_id>/images",
  "datasource": {"source": "images"}
}

供参考,请参阅Multiple API Endpoints, One Datasource