文档服务 API:填充字段
🌐 Document Service API: Populating fields
Page summary:使用 Document Service API 的
populate参数可以显式加载关系、媒体字段、组件和动态区域,支持一层或多层深度,并在create()、update()、publish()和delete()操作中使用。🌐 Use the
populateparameter with the Document Service API to explicitly load relations, media fields, components, and dynamic zones at one or multiple levels deep, and withincreate(),update(),publish(), anddelete()operations.
默认情况下,文档服务 API 不会填充任何关系、媒体字段、组件或动态区域。本页介绍如何使用 populate 参数来填充特定字段。
🌐 By default the Document Service API does not populate any relations, media fields, components, or dynamic zones. This page describes how to use the populate parameter to populate specific fields.
你也可以使用 fields 参数只返回查询结果中的特定字段(参见 fields 参数 文档)。
🌐 You can also use the fields parameter to return only specific fields with the query results (see the fields parameter documentation).
如果启用了用户与权限功能,则必须为正在填充的内容类型启用 find 权限。如果某个角色无法访问某个内容类型,则该内容类型将不会被填充。
🌐 If the Users & Permissions feature is enabled, 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.
status、publicationFilter 和 hasPublishedVersion 参数只能在根级别使用,并且适用于整个查询。它们不能放在嵌套的 populate 对象里,如果尝试这样做会返回验证错误。
🌐 The status, publicationFilter, and hasPublishedVersion parameters are root-level only and apply to the entire query. They cannot be used inside nested populate objects and will return a validation error if attempted.
关系与媒体字段
🌐 Relations and media fields
查询可以接受一个 populate 参数来显式定义要填充的字段,以下是语法选项示例。这包括所有关系类型:一对多、多对一、多对多以及多态关系(morphToOne、morphToMany)。
🌐 Queries can accept a populate parameter to explicitly define which fields to populate, with the following syntax option examples. This includes all relation types: one-to-many, many-to-one, many-to-many, and polymorphic relations (morphToOne, morphToMany).
为所有关系填充 1 级
🌐 Populate 1 level for all relations
strapi.documents("api::article.article").findMany()为所有关系填充 1 级
使用通配符填充所有关系的一层深度。
- JavaScript
const documents = await strapi.documents("api::article.article").findMany({
populate: "*",
});
{
[
{
"id": "cjld2cjxh0000qzrmn831i7rn",
"title": "Test Article",
"slug": "test-article",
"body": "Test 1",
// ...
"headerImage": {
"data": {
"id": 1,
"attributes": {
"name": "17520.jpg",
"alternativeText": "17520.jpg",
"formats": {
// ...
}
// ...
}
}
},
"author": {
// ...
},
"categories": {
// ...
}
}
// ...
]
}