Skip to main content

REST API:人口和字段选择

¥REST API: Population & Field Selection

默认情况下,REST API 不填充任何关系、媒体字段、组件或动态区域。使用 populate 参数 填充特定字段,使用 select 参数 仅返回特定字段和查询结果。确保为你填充的关系的字段授予查找权限。

¥The REST API by default does not populate any relations, media fields, components, or dynamic zones. Use the populate parameter to populate specific fields and the select parameter to return only specific fields with the query results. Ensure that the find permission is given to the field(s) for the relation(s) you populate.

💡 提示

Strapi 利用 qs 解析嵌套对象的能力来创建更复杂的查询。

¥Strapi takes advantage of the ability of the qs library to parse nested objects to create more complex queries.

直接使用 qs 生成复杂的查询,而不是手动创建它们。本文档中的示例展示了如何使用 qs

¥Use qs directly to generate complex queries instead of creating them manually. Examples in this documentation showcase how you can use qs.

如果你更喜欢使用我们的在线工具而不是在计算机上使用 qs 生成查询,你也可以使用 交互式查询构建器

¥You can also use the interactive query builder if you prefer playing with our online tool instead of generating queries with qs on your machine.

字段选择

¥Field selection

查询可以接受 fields 参数来仅选择某些字段。默认情况下,仅返回以下 字段类型

¥Queries can accept a fields parameter to select only some fields. By default, only the following types of fields are returned:

  • 字符串类型:字符串、文本、富文本、枚举、电子邮件、密码和 uid,

    ¥string types: string, text, richtext, enumeration, email, password, and uid,

  • 日期类型:日期、时间、日期时间和时间戳,

    ¥date types: date, time, datetime, and timestamp,

  • 号码类型:整数、大整数、浮点数和小数,

    ¥number types: integer, biginteger, float, and decimal,

  • 通用类型:布尔值、数组和 JSON。

    ¥generic types: boolean, array, and JSON.

字段选择不适用于关系、媒体、组件或动态区域字段。要填充这些字段,请使用 populate 参数

¥Field selection does not work on relational, media, component, or dynamic zone fields. To populate these fields, use the populate parameter.

💡 提示

默认情况下,会选择除关系、媒体、动态区域和组件之外的字段,但你可以指定通配符 * 而不是数组。

¥By default, fields are selected except relations, media, dynamic zones, and components, but you can specify a wildcard * instead of an array.



Example request: Return only title and body fields

GET /api/users?fields[0]=title&fields[1]=body

Example response
{
"data": [
{
"id": 1,
"attributes": {
"title": "test1",
"body": "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
}
}
],
"meta": {
// ...
}
}

JavaScript 查询(使用 qs 库构建):

¥JavaScript query (built with the qs library):

上面的查询 URL 是使用 qs 构建的。qs 可以在你的计算机上本地运行,如以下代码示例所示,或者你也可以使用我们的 交互式查询构建器 在线工具。

¥The query URL above was built using the qs library. qs can be run locally on your machine, as shown in the following code example, or you can use our interactive query builder online tool.



const qs = require('qs');




const query = qs.stringify(


{
fields: ['title', 'body'],
},
{
encodeValuesOnly: true, // prettify URL
}
);

await request(`/api/users?${query}`);

人口

¥Population

默认情况下,REST API 不会填充任何类型的字段,因此它不会填充关系、媒体字段、组件或动态区域,除非你传递 populate 参数来填充各种字段类型。

¥The REST API by default does not populate any type of fields, so it will not populate relations, media fields, components, or dynamic zones unless you pass a populate parameter to populate various field types.

populate 参数可以单独使用,也可以使用 与多个操作符结合 对总体进行更多控制。

¥The populate parameter can be used alone or in combination with with multiple operators to have much more control over the population.

提醒

必须为正在填充的内容类型启用 find 权限。如果角色无权访问内容类型,则不会填充该角色(有关如何为内容类型启用 find 权限的其他信息,请参阅 用户指南)。

¥The find permission must be enabled for the content-types that are being populated. If a role doesn't have access to a content-type it will not be populated (see User Guide for additional information on how to enable find permissions for content-types).

✏️ 注意

目前无法仅通过请求返回 id 数组。

¥It's currently not possible to return just an array of ids with a request.

🤓 填充指南

REST API 指南 部分包含有关填充参数的各种可能用例的更详细信息:

¥The REST API guides section includes more detailed information about various possible use cases for the populate parameter:

  • 了解填充 指南通过图表、比较和实际示例详细解释了填充的工作原理。

    ¥The Understanding populate guide explains in details how populate works, with diagrams, comparisons, and real-world examples.

  • 如何填充创建者字段 指南提供了有关如何将 createdByupdatedBy 字段添加到查询响应中的分步说明。

    ¥The How to populate creator fields guide provides step-by-step instructions on how to add createdBy and updatedBy fields to your queries responses.

Strapi 博客还包含有关 如何使用查询填充和过滤数据 的教程。

¥The Strapi Blog also includes a tutorial on how to populate and filter data with your queries.

下表总结了可能的填充用例及其关联的参数语法,并链接到了解填充指南的各部分,其中包括更详细的说明:

¥The following table sums up possible populate use cases and their associated parameter syntaxes, and links to sections of the Understanding populate guide which includes more detailed explanations:

使用案例参数语法示例详细解释请阅读
填充所有内容,深度为 1 层,包括媒体字段、关系、组件和动态区域populate=*填充所有关系和字段,深 1 层
填充一个关系,
1 层深
populate[0]=a-relation-name为特定关系填充 1 层深度
填充多个关系,
深 1 层
populate[0]=relation-name&populate[1]=another-relation-name&populate[2]=yet-another-relation-name为特定关系填充 1 层深度
填充一些深层次的关系populate[first-level-relation-to-populate][populate][0]=second-level-relation-to-populate为特定关系填充多个深度级别
填充组件populate[0]=component-name填充组件
填充组件及其嵌套组件之一populate[0]=component-name&populate[1]=component-name.nested-component-name填充组件
填充动态区域(仅填充其第一级元素)populate[0]=dynamic-zone-name填充动态区域
使用独特的共享填充策略填充动态区域及其嵌套元素和关系populate[dynamic-zone-name][populate]=*填充动态区域
使用精确定义、详细的填充策略填充动态区域及其嵌套元素和关系populate[dynamic-zone-name][on][dynamic-zone-name.component-name][populate][relation-name][populate][0]=field-name填充动态区域
💡 提示

构建具有多级填充的复杂查询的最简单方法是使用我们的 交互式查询构建器 工具。

¥The easiest way to build complex queries with multiple-level population is to use our interactive query builder tool.

将 Population 与其他运算符结合起来

¥Combining Population with other operators

通过利用 populate 运算符,可以在总体查询中组合其他运算符,例如 字段选择filterssort

¥By utilizing the populate operator it is possible to combine other operators such as field selection, filters, and sort in the population queries.

提醒

总体和分页运算符不能组合使用。

¥The population and pagination operators cannot be combined.

使用字段选择填充

¥Populate with field selection

fieldspopulate 可以组合。

¥fields and populate can be combined.

Example request

GET /api/articles?fields[0]=title&fields[1]=slug&populate[headerImage][fields][0]=name&populate[headerImage][fields][1]=url

Example response
{
"data": [
{
"id": 1,
"attributes": {
"title": "Test Article",
"slug": "test-article",
"headerImage": {
"data": {
"id": 1,
"attributes": {
"name": "17520.jpg",
"url": "/uploads/17520_73c601c014.jpg"
}
}
}
}
}
],
"meta": {
// ...
}
}

JavaScript 查询(使用 qs 库构建):

¥JavaScript query (built with the qs library):

上面的查询 URL 是使用 qs 构建的。qs 可以在你的计算机上本地运行,如以下代码示例所示,或者你也可以使用我们的 交互式查询构建器 在线工具。

¥The query URL above was built using the qs library. qs can be run locally on your machine, as shown in the following code example, or you can use our interactive query builder online tool.



const qs = require('qs');




const query = qs.stringify(


{
fields: ['title', 'slug'],
populate: {
headerImage: {
fields: ['name', 'url'],
},
},
},
{
encodeValuesOnly: true, // prettify URL
}
);

await request(`/api/articles?${query}`);

填充过滤

¥Populate with filtering

filterspopulate 可以组合。

¥filters and populate can be combined.

Example request

GET /api/articles?populate[categories][sort][0]=name%3Aasc&populate[categories][filters][name][$eq]=Cars

Example response
{
"data": [
{
"id": 1,
"attributes": {
"title": "Test Article",
// ...
"categories": {
"data": [
{
"id": 2,
"attributes": {
"name": "Cars"
// ...
}
}
]
}
}
}
],
"meta": {
// ...
}
}

JavaScript 查询(使用 qs 库构建):

¥JavaScript query (built with the qs library):

上面的查询 URL 是使用 qs 构建的。qs 可以在你的计算机上本地运行,如以下代码示例所示,或者你也可以使用我们的 交互式查询构建器 在线工具。

¥The query URL above was built using the qs library. qs can be run locally on your machine, as shown in the following code example, or you can use our interactive query builder online tool.



const qs = require('qs');




const query = qs.stringify(


{
populate: {
categories: {
sort: ['name:asc'],
filters: {
name: {
$eq: 'Cars',
},
},
},
},
},
{
encodeValuesOnly: true, // prettify URL
}
);

await request(`/api/articles?${query}`);