(api_v2_usage)= # Wagtail API v2 使用指南 ```{warning} v2 API 计划在未来的版本中弃用。见[弃用时间表](api_v2_deprecation),新项目请考虑使用 [v3 API](api_v3)。 ``` Wagtail API 模块对外暴露一个公开的、只读的、JSON 格式的 API, 可供外部客户端(例如移动应用)或站点前端使用。 本文档面向使用 Wagtail 所暴露 API 的开发者。关于如何在你的 Wagtail 站点中启用 API 模块,见 [Wagtail API v2 配置指南](/advanced_topics/api/v2/configuration) 目录 ```{contents} --- local: depth: 3 --- ``` ## 获取内容 要通过 API 获取内容,请对下列端点之一 发起 `GET` 请求: - 页面 `/api/v2/pages/` - 图片 `/api/v2/images/` - 文档 `/api/v2/documents/` ```{note} 可用端点及其 URL 可能因站点而异,取决于 API 是如何配置的。 ``` ### 响应示例 每个响应都包含条目列表(`items`)和总数 (`meta.total_count`)。总数与分页无关。 ```text GET /api/v2/endpoint_name/ HTTP 200 OK Content-Type: application/json { "meta": { "total_count": "total number of results" }, "items": [ { "id": 1, "meta": { "type": "app_name.ModelName", "detail_url": "https://api.example.com/api/v2/endpoint_name/1/" }, "field": "value" }, { "id": 2, "meta": { "type": "app_name.ModelName", "detail_url": "https://api.example.com/api/v2/endpoint_name/2/" }, "field": "different value" } ] } ``` (apiv2_custom_page_fields)= ### API 中的自定义页面字段 Wagtail 站点包含许多页面类型,每种都有自己的字段集。 `pages` 端点默认只暴露通用字段(例如 `title` 和 `slug`)。 要通过 API 访问自定义页面字段,请用 `?type` 参数 选择页面类型。这会过滤结果,使其只包含该类型的页面, 同时也会让该类型所有已导出的自定义字段 在 API 中可用。 例如,要访问[配置文档](apiv2_page_fields_configuration)中所述 `blog.BlogPage` 模型上的 `published_date`、`body` 和 `authors` 字段: ``` GET /api/v2/pages/?type=blog.BlogPage&fields=published_date,body,authors(name) HTTP 200 OK Content-Type: application/json { "meta": { "total_count": 10 }, "items": [ { "id": 1, "meta": { "type": "blog.BlogPage", "detail_url": "https://api.example.com/api/v2/pages/1/", "html_url": "https://www.example.com/blog/my-blog-post/", "slug": "my-blog-post", "first_published_at": "2016-08-30T16:52:00Z" }, "title": "Test blog post", "published_date": "2016-08-30", "authors": [ { "id": 1, "meta": { "type": "blog.BlogPageAuthor", }, "name": "Karl Hobley" } ] }, ... ] } ``` ```{note} 只有在开发者显式导出的字段才能在 API 中使用。 这是通过在页面模型上添加 `api_fields` 属性完成的。 你可以[在这里](apiv2_page_fields_configuration)阅读相关配置。 ``` 这不适用于图片/文档,因为那些端点中只暴露一个模型。 但对于定制了图片/文档模型的项目,可以用 `api_fields` 属性把任何自定义字段导出到 API。 (apiv2_pagination)= ### 分页 响应中的条目数量可以通过 `?limit` 参数改变 (默认:20),要跳过的条目数量可以通过 `?offset` 参数改变。 例如: ``` GET /api/v2/pages/?offset=20&limit=20 HTTP 200 OK Content-Type: application/json { "meta": { "total_count": 50 }, "items": [ pages 20 - 40 will be listed here. ] } ``` ```{note} `?limit` 参数可能有最大值限制。你可以在项目设置中 把 `WAGTAILAPI_LIMIT_MAX` 设为某个数字(新的最大值)或 `None`(禁用最大值检查)来修改它。 ``` (api_v2_usage_ordering)= ### 排序 把 `?order` 参数设为要排序的字段名, 即可按任何字段对结果排序。 ``` GET /api/v2/pages/?order=title HTTP 200 OK Content-Type: application/json { "meta": { "total_count": 50 }, "items": [ pages will be listed here in ascending title order (a-z) ] } ``` 默认按升序排列。在字段名前加 `-` 号可以改为降序。 ``` GET /api/v2/pages/?order=-title HTTP 200 OK Content-Type: application/json { "meta": { "total_count": 50 }, "items": [ pages will be listed here in descending title order (z-a) ] } ``` ```{note} 排序区分大小写,因此在升序排列时,小写字母总是排在 大写字母之后。 ``` #### 多字段排序 可以把多个字段传给 `?order` 以进行连续排序。 ``` GET /api/v2/pages/?order=title,-slug HTTP 200 OK Content-Type: application/json { "meta": { "total_count": 50 }, "items": [ pages will be ordered by title and for all matching titles (a-z), then sorted by slug (z-a). ] } ``` #### 随机排序 把 `random` 传入 `?order` 参数会让结果以 随机顺序返回。在没有缓存的情况下,每次请求都会以不同顺序 返回结果。 ``` GET /api/v2/pages/?order=random HTTP 200 OK Content-Type: application/json { "meta": { "total_count": 50 }, "items": [ pages will be listed here in random order ] } ``` ```{note} 随机排序时不能同时使用 `?offset`,因为跨多个请求 无法保证一致的随机顺序(因此后续分页请求返回的结果 可能也出现在之前的页面中)。 ``` ### 过滤 任何字段都可以用于精确匹配过滤。用过滤名作为 参数名,把要匹配的值作为参数值。 例如,要查找 slug 为 "about" 的页面: ``` GET /api/v2/pages/?slug=about HTTP 200 OK Content-Type: application/json { "meta": { "total_count": 1 }, "items": [ { "id": 10, "meta": { "type": "standard.StandardPage", "detail_url": "https://api.example.com/api/v2/pages/10/", "html_url": "https://www.example.com/about/", "slug": "about", "first_published_at": "2016-08-30T16:52:00Z" }, "title": "About" }, ] } ``` (apiv2_filter_by_tree_position)= ### 按树中位置过滤(仅页面) 页面还可以按其与树中其它页面的关系来过滤。 `?child_of` 过滤器接收一个页面的 id,把结果列表过滤为 只包含该页面的直接子页面。 例如,这在构造主菜单时很有用 —— 把首页的 id 传给该过滤器: ``` GET /api/v2/pages/?child_of=2&show_in_menus=true HTTP 200 OK Content-Type: application/json { "meta": { "total_count": 5 }, "items": [ { "id": 3, "meta": { "type": "blog.BlogIndexPage", "detail_url": "https://api.example.com/api/v2/pages/3/", "html_url": "https://www.example.com/blog/", "slug": "blog", "first_published_at": "2016-09-21T13:54:00Z" }, "title": "About" }, { "id": 10, "meta": { "type": "standard.StandardPage", "detail_url": "https://api.example.com/api/v2/pages/10/", "html_url": "https://www.example.com/about/", "slug": "about", "first_published_at": "2016-08-30T16:52:00Z" }, "title": "About" }, ... ] } ``` `?ancestor_of` 过滤器接收一个页面的 id,把列表过滤为 只包含该页面的祖先(父页面、祖父页面等等),一直 向上到站点的根页面。 例如,与 `type` 过滤器结合使用时,可以用它找出 某个 `blog.BlogPage` 所属的具体 `blog.BlogIndexPage`。 单独使用时,它可以用来构造从当前页面 回到站点根页面的面包屑路径。 `?descendant_of` 过滤器接收一个页面的 id,把列表过滤为 只包含该页面的子代(子页面、孙页面等等)。 (api_filtering_pages_by_site)= ### 按站点过滤页面 默认情况下,API 会根据请求的主机名查找站点。 某些情况下,你可能想查询属于另一个站点的页面。 `?site=` 过滤器用于把列表过滤为只包含属于 特定站点的页面。该过滤器需要站点已配置的主机名。 如果你有多个站点使用相同主机名但端口号不同,可以用 `hostname:port` 的格式按端口号过滤。例如: ``` GET /api/v2/pages/?site=demo-site.local GET /api/v2/pages/?site=demo-site.local:8080 ``` ### 搜索 给 `?search` 参数传一个查询,就会对结果 执行全文搜索。 查询会被切分成若干「词条」(按词边界),然后每个词条会被规范化 (转小写并去除重音符号)。 例如:`?search=James+Joyce` #### 搜索运算符 `search_operator` 指定查询中的多个词条应如何处理。 有两个可选值: - `and` —— 查询中的所有词条(排除停用词)都必须出现在 每个结果中 - `or` —— 查询中至少有一个词条出现在每个结果中 `or` 运算符通常比 `and` 更好,因为它允许用户的查询不那么精确, 而排序算法会确保不相关的结果不会出现在 页面顶部。 默认的搜索运算符取决于站点所用的搜索引擎是否 支持排序。如果支持(Elasticsearch),运算符默认为 `or`。否则(数据库),默认为 `and`。 出于同样的原因,把 `?search` 与 `?order` 一起使用时 也建议使用 `and` 运算符(因为这会禁用排序)。 例如:`?search=James+Joyce&order=-first_published_at&search_operator=and` (apiv2_i18n_filters)= ### 国际化站点的专用过滤器 当 `WAGTAIL_I18N_ENABLED` 设为 `True` 时(更多细节见 [](enabling_internationalisation)),pages 端点上会多出 两个可用的过滤器。 #### 按区域设置过滤页面 `?locale=` 过滤器用于把列表过滤为只包含处于 指定区域设置的页面。例如: ``` GET /api/v2/pages/?locale=en-us HTTP 200 OK Content-Type: application/json { "meta": { "total_count": 5 }, "items": [ { "id": 10, "meta": { "type": "standard.StandardPage", "detail_url": "https://api.example.com/api/v2/pages/10/", "html_url": "https://www.example.com/usa-page/", "slug": "usa-page", "first_published_at": "2016-08-30T16:52:00Z", "locale": "en-us" }, "title": "American page" }, ... ] } ``` #### 获取页面的翻译版本 `?translation_of` 过滤器用于把列表过滤为只包含 指定页面 ID 的翻译版本的页面。例如: ``` GET /api/v2/pages/?translation_of=10 HTTP 200 OK Content-Type: application/json { "meta": { "total_count": 2 }, "items": [ { "id": 11, "meta": { "type": "standard.StandardPage", "detail_url": "https://api.example.com/api/v2/pages/11/", "html_url": "https://www.example.com/gb-page/", "slug": "gb-page", "first_published_at": "2016-08-30T16:52:00Z", "locale": "en-gb" }, "title": "British page" }, { "id": 12, "meta": { "type": "standard.StandardPage", "detail_url": "https://api.example.com/api/v2/pages/12/", "html_url": "https://www.example.com/fr-page/", "slug": "fr-page", "first_published_at": "2016-08-30T16:52:00Z", "locale": "fr" }, "title": "French page" }, ] } ``` ### 字段 默认情况下,响应中只返回可用字段的一个子集。 `?fields` 参数既可用于向响应中添加额外字段, 也可用于移除你知道自己不需要的默认字段。 #### 添加字段 把 `?fields` 设为你想要添加的字段名组成的逗号分隔列表, 即可向响应中添加额外字段。 例如,`?fields=body,feed_image` 会把 `body` 和 `feed_image` 字段添加到响应中。 这也可以跨越关系使用。例如, `?fields=body,feed_image(width,height)` 会在响应中嵌套图片的 `width` 和 `height`。 #### 所有字段 把 `?fields` 设为星号(`*`)会把所有可用字段添加到 响应中。这在探查哪些字段已被导出时很有用。 例如:`?fields=*` #### 移除字段 在字段名前加上 `-` 并加入 `?fields`, 即可移除你知道自己不需要的字段。 例如,`?fields=-title,body` 会移除 `title` 并添加 `body`。 这也可以与星号一起使用。例如,`?fields=*,-body` 添加除 `body` 之外的所有字段。 #### 移除所有默认字段 要精确指定你需要的字段,可以把 fields 中的第一项设为 下划线(`_`),这会移除所有默认字段。 例如,`?fields=_,title` 只会返回 title 字段。 ### 详情视图 把对象的 id 追加到 URL 末尾,即可从 API 获取 单个对象。例如: - 页面 `/api/v2/pages/1/` - 图片 `/api/v2/images/1/` - 文档 `/api/v2/documents/1/` 默认情况下,响应中会返回所有已导出的字段。你可以用 `?fields` 参数定制显示哪些字段。 例如:`/api/v2/pages/1/?fields=_,title,body` 只会返回 id 为 1 的页面的 `title` 和 `body`。 (apiv2_finding_pages_by_path)= ### 按 HTML 路径查找页面 你可以用 `/api/v2/pages/find/?html_path=` 视图按页面的 HTML 路径 查找单个页面。 它会返回 `302` 重定向到该页面的详情视图,或返回 `404` 未找到响应。 例如:`/api/v2/pages/find/?html_path=/` 总是重定向到站点的首页 ## 默认端点字段 ### 通用字段 每个端点都会返回这些字段。 **`id`(数字)** 对象的唯一 ID ```{note} 除页面类型外,其它每种内容类型都有各自的 ID 空间, 因此你必须把它与 ``type`` 字段结合,才能得到 对象的唯一标识。 ``` **`type`(字符串)** 对象的类型,格式为 `app_label.ModelName` **`detail_url`(字符串)** 该对象详情视图的 URL ### 页面 **`title`(字符串)** **`meta.slug`(字符串)** **`meta.show_in_menus`(布尔值)** **`meta.seo_title`(字符串)** **`meta.search_description`(字符串)** **`meta.first_published_at`(日期/时间)** 这些值取自页面上的对应字段 **`meta.html_url`(字符串)** 如果站点有由 Wagtail 生成的 HTML 前端,该 字段会被设为该页面的 URL **`meta.parent`** 嵌套了父页面的一些信息(仅在详情视图中 可用) **`meta.alias_of`(字典)** 如果该页面被标记为别名,返回原始页面的 ID 和完整 URL ### 图片 **`title`(字符串)** 图片 title 字段的值。在 Wagtail 内部,它被用于 图片的 `alt` HTML 属性。 **`width`(数字)** **`height`(数字)** 原始图片文件的尺寸 **`meta.tags`(字符串列表)** 与该图片关联的标签列表 ### 文档 **`title`(字符串)** 文档 title 字段的值 **`meta.tags`(字符串列表)** 与该文档关联的标签列表 **`meta.download_url`(字符串)** 指向文档文件的 URL ## 相对 v1 的变化 ### 破坏性变更 - 列表响应中的结果列表已改名为 `items`(此前是 `pages`、`images` 或 `documents`) ### 主要功能 - `fields` 参数经过改进,支持移除字段、添加所有字段以及定制嵌套字段 ### 次要功能 - pages 端点新增了 `html_url`、`slug`、`first_published_at`、`expires_at` 和 `show_in_menus` 字段 - documents 端点新增了 `download_url` 字段 - page 端点的 `type` 参数可以指定多个页面类型 - 过滤布尔字段时现在可以使用 `true` 和 `false` - `order` 现在可以与 `search` 一起使用 - 新增了 `search_operator` 参数