模型
🌐 Models
Page summary:模型通过内容类型和可重用组件定义 Strapi 的内容结构。本指南将介绍如何在内容类型构建器或 CLI 中创建这些模型,以及如何使用可选的生命周期钩子管理模式文件。
由于 Strapi 是一个无头内容管理系统(CMS),为内容创建内容结构是使用该软件时最重要的方面之一。模型定义了内容结构的表示。
🌐 As Strapi is a headless Content Management System (CMS), creating a content structure for the content is one of the most important aspects of using the software. Models define a representation of the content structure.
Strapi 有 2 种不同类型的模型:
🌐 There are 2 different types of models in Strapi:
- 内容类型,可以是集合类型或单一类型,具体取决于它们管理的条目数量,
- 以及可在多种内容类型中重复使用的内容结构组件。
如果你刚刚开始,可以直接在管理面板中使用 Content-type Builder 生成一些模型。这种用户界面承担了许多验证任务,并展示了创建内容结构的所有可用选项。然后可以使用此文档在代码层面上查看生成的模型映射。
🌐 If you are just starting out, it is convenient to generate some models with the Content-type Builder directly in the admin panel. The user interface takes over a lot of validation tasks and showcases all the options available to create the content's content structure. The generated model mappings can then be reviewed at the code level using this documentation.
模型创建
🌐 Model creation
内容类型和组件模型的创建和存储方式不同。
🌐 Content-types and components models are created and stored differently.
内容类型
🌐 Content-types
可以在 Strapi 中创建内容类型:
🌐 Content-types in Strapi can be created:
- 在管理面板的内容类型构建器中,
- 或者使用 Strapi 的交互式 CLI
strapi generate命令。
内容类型使用以下文件:
🌐 The content-types use the following files:
这些模型文件存储在 ./src/api/[api-name]/content-types/[content-type-name]/ 中,任何在这些文件夹中找到的 JavaScript 或 JSON 文件都将被加载为内容类型的模型(参见 项目结构)。
🌐 These models files are stored in ./src/api/[api-name]/content-types/[content-type-name]/, and any JavaScript or JSON file found in these folders will be loaded as a content-type's model (see project structure).
内容类型的存储方式并不取决于它们在管理面板中的显示方式。内容类型可以分组到文件夹中,这些文件夹在一个单独的内容结构文件中进行描述。
🌐 How content-types are stored does not depend on how they are displayed in the admin panel. Content-types can be grouped into folders, and these folders are described in a separate content structure file.
在启用了 TypeScript 的项目中,可以使用 ts:generate-types 命令生成模式类型定义。
🌐 In TypeScript-enabled projects, schema typings can be generated using the ts:generate-types command.
组件
🌐 Components
组件模型无法使用 CLI 工具创建。请使用 内容类型构建器 或手动创建它们。
🌐 Component models can't be created with CLI tools. Use the Content-type Builder or create them manually.
组件模型存储在 ./src/components 文件夹中。每个组件必须位于一个子文件夹内,该子文件夹的名称应与组件所属的类别相同(参见 项目结构)。
🌐 Components models are stored in the ./src/components folder. Every component has to be inside a subfolder, named after the category the component belongs to (see project structure).
组件也可以接受可选的预览图片,在动态区域选择器中显示,而不是它们的图标(参见 组件预览图片)。
🌐 Components also accept an optional preview image, displayed in the dynamic zone picker instead of their icon (see Component preview images).
模型架构
🌐 Model schema
一个模型的 schema.json 文件包含:
🌐 The schema.json file of a model consists of:
- 设置,例如模型表示的内容类型或应存储数据的表名,
- 信息,主要用于在管理面板中显示模型并通过 REST 和 GraphQL API 访问它,
- 属性,描述模型的内容结构,
- 以及 options 用于定义模型上的特定行为。
模型设置
🌐 Model settings
模型的常规设置可以使用以下参数进行配置:
🌐 General settings for the model can be configured with the following parameters:
| 参数 | 类型 | 描述 |
|---|---|---|
collectionName | 字符串 | 数据应存储的数据库表名称 |
kind可选, 仅用于内容类型 | 字符串 | 定义内容类型是否为:
|
// ./src/api/[api-name]/content-types/restaurant/schema.json
{
"kind": "collectionType",
"collectionName": "Restaurants_v1",
}
模型信息
🌐 Model information
模型模式中的 info 键描述了用于在管理面板中显示模型以及通过内容 API 访问模型的信息。它包括以下参数:
🌐 The info key in the model's schema describes information used to display the model in the admin panel and access it through the Content API. It includes the following parameters:
| 参数 | 类型 | 描述 |
|---|---|---|
displayName | 字符串 | 在管理面板中使用的默认名称 |
singularName | 字符串 | 内容类型名称的单数形式。 用于生成 API 路由和数据库/表集合。 应使用 kebab-case 格式。 |
pluralName | 字符串 | 内容类型名称的复数形式。 用于生成 API 路由和数据库/表集合。 应使 用 kebab-case 格式。 |
description | 字符串 | 模型的描述 |
icon | 字符串 | 用于在管理面板中表示模型的 Strapi 图标 名称 |
preview | 字符串 | 用于在管理面板中表示组件的图片路径或 URL。 仅限组件。请参阅 组件预览图片。 |
这个 preview 参数与 预览功能 无关。这个功能用于预览前端内容,并使用 config/admin 的 preview 对象。
🌐 This preview parameter is unrelated to the Preview feature.
That feature previews front-end content, and uses the preview object of config/admin.
"info": {
"displayName": "Restaurant",
"singularName": "restaurant",
"pluralName": "restaurants",
"description": ""
},
组件预览图片
🌐 Component preview images
组件在它们的 info 对象中可以接受一个可选的 preview 参数。它指向一个在 动态区域选择器 中表示组件的图片。
🌐 Components accept an optional preview parameter in their info object. It points to an image representing the component in the dynamic zone picker.
preview 参数接受:
🌐 The preview parameter accepts:
- 一个相对于根目录的路径,指向放在项目
public目录下的图片,例如/_component-screenshots/hero-section.png。图片是由public中间件 提供的,该中间件不会提供以/uploads/开头的路径。 - 指向外部图片托管的绝对 URL。
媒体库 的图片是从 /uploads/ 提供的,所以不能用作预 览图。请把文件提交到 public。
{
"info": {
"displayName": "Hero Section",
"icon": "layout",
"preview": "/_component-screenshots/hero-section.png"
}
}
当省略 preview,或者图片加载失败时,管理面板会回退到组件的 icon。
🌐 When preview is omitted, or when the image fails to load, the admin panel falls back to the component's icon.
preview 参数必须在组件的 schema 文件中手动设置。内容类型构建器目前还无法上传预览图片。
🌐 The preview parameter must be set manually in the component's schema file. The Content-type Builder cannot upload a preview image yet.
模型属性
🌐 Model attributes
模型的内容结构由一系列属性组成。每个属性都有一个 type 参数,用于描述其性质,并将该属性定义为简单的数据片段或 Strapi 使用的更复杂结构。
🌐 The content structure of a model consists of a list of attributes. Each attribute has a type parameter, which describes its nature and defines the attribute as a simple piece of data or a more complex structure used by Strapi.
有多种类型的属性可用:
🌐 Many types of attributes are available:
- 标量类型(例如字符串、日期、数字、布尔值等),
- Strapi 特有的类型,例如:
属性的 type 参数应为以下值之一:
🌐 The type parameter of an attribute should be one of the following values:
| 类型类别 | 可用类型 |
|---|---|
| 字符串类型 |
|
| 日期类型 |
|
| 数字类型 |
|
| 其他通用类型 |
|
| Strapi 独有的特殊类型 | |
| 国际化 (i18n) 相关类型 仅在内容类型启用了 i18n 时可用 |
|
验证
🌐 Validations
可以使用以下参数将基本验证应用于属性:
🌐 Basic validations can be applied to attributes using the following parameters:
| 参数 | 类型 | 描述 | 默认值 |
|---|---|---|---|
required | 布尔值 | 如果 true,为此属性添加必填验证器 | false |
max | 整数 | 检查值是否大于或等于给定的最大值 | - |
min | 整数 | 检查值是否小于或等于给定的最小值 | - |
minLength | 整数 | 字段输入值的最小字符数 | - |
maxLength | 整数 | 字段输入值的最大字符数 | - |
private | 布尔值 | 如果 true,该属性将从服务器响应中移除。💡 这对于隐藏敏感数据很有用。 | false |
configurable | 布尔值 | 如果 false,该属性无法通过内容类型构建器插件进行配置。 | true |
{
// ...
"attributes": {
"title": {
"type": "string",
"minLength": 3,
"maxLength": 99,
"unique": true
},
"description": {
"default": "My description",
"type": "text",
"required": true
},
"slug": {
"type": "uid",
"targetField": "title"
}
// ...
}
}
数据库验证和设置
🌐 Database validations and settings
这些设置应该保留给高级使用,因为它们可能会导致某些功能失效。目前没有计划让这些设置稳定。
🌐 These settings should be reserved to an advanced usage, as they might break some features. There are no plans to make these settings stable.
数据库验证和设置是直接传递给 tableBuilder Knex.js 函数的自定义选项,用于架构迁移期间。数据库验证允许对设置自定义列设置进行高级控制。以下选项在每个属性的 column: {} 对象中设置:
🌐 Database validations and settings are custom options passed directly onto the tableBuilder Knex.js function during schema migrations. Database validations allow for an advanced degree of control for setting custom column settings. The following options are set in a column: {} object per attribute:
| 参数 | 类型 | 描述 | 默认值 |
|---|---|---|---|
name | 字符串 | 更改数据库中列的名称 | - |
defaultTo | 字符串 | 设置数据库的 defaultTo,通常与 notNullable 一起使用 | - |
notNullable | 布尔值 | 设置数据库的 notNullable,确保列不能为空 | false |
unsigned | 布尔值 | 仅适用于数字列,移除允许负数的功能,但将最大长度加倍 | false |
unique |