跳到正文
Skillpedia
API 与服务器(后端)8.1k · #12598

webiny-api-cms-content-models

它能做什么

这个技能指导你的 AI 通过代码创建或修改 Webiny 项目中的 CMS 内容模型。适用于希望把数据结构写在受版本控制代码中的开发者。

适用场景

  • ✓希望用代码创建产品或博客文章等内容模型的时候。
  • ✓字段需要验证或引用其他模型的时候。
  • ✓需要为某种字段类型选择合适的管理后台显示方式时。

技能链接

https://github.com/webiny/webiny-js/blob/next/skills/user-skills/content-models/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/webiny/webiny-js/blob/next/skills/user-skills/content-models/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“请帮我用代码创建一个产品内容模型,包含名称、价格,以及对分类模型的引用。”

每周 5 个新技能,1 分钟读完。

工作原理

该技能展示了一个模型类,它使用流式构建器声明字段、布局、标题字段和 API 名称。该模型在项目配置文件中注册。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Creating Content Models via Code

TL;DR

Content models are created using the ModelFactory pattern. You define a class implementing ModelFactory.Interface, use the fluent ModelFactory.Builder API to declare fields, validators, layout, and API names, then export with ModelFactory.createImplementation(). Register in webiny.config.tsx as <Api.Extension>.

The ModelFactory Pattern

Every code-based content model follows the same structure:

import { ModelFactory } from "webiny/api/cms/model";

class MyModelImpl implements ModelFactory.Interface {
  async execute(builder: ModelFactory.Builder) {
    return [
      builder
        .public({ modelId: "myModel", name: "My Model", group: "ungrouped" })
        .description("Description of the model")
        .fields(fields => ({
          // field definitions here
        }))
        .layout([/* row definitions */])
        .titleFieldId("fieldId")
        .singularApiName("MyModel")
        .pluralApiName("MyModels")
    ];
  }
}

export default ModelFactory.createImplementation({
  implementation: MyModelImpl,
  dependencies: []
});

Register in webiny.config.tsx:

<Api.Extension src={"/extensions/MyModel.ts"} />

YOU MUST include the full file path with the .ts extension in the src prop. For example, use src={"/extensions/MyModel.ts"}, NOT src={"/extensions/MyModel"}. Omitting the file extension will cause a build failure.

YOU MUST use export default for the createImplementation() call when the file is targeted directly by an Extension src prop. Using a named export (export const MyModel = ...) will cause a build failure. Named exports are only valid inside files registered via createFeature.

Model Configuration Methods

Method Purpose
.public({ modelId, name, group }) Creates a public model (accessible via Read API). modelId is the internal DB identifier. group organizes it in the Admin sidebar.
.description("...") Model description shown in Admin UI
.fields(fields => ({ ... })) Define all fields using the fluent field builder
.layout([["field1", "field2"], ["field3"]]) Arrange fields in rows in the Admin editor. Each inner array is one row.
.titleFieldId("name") Which field to use as the entry's display title
.descriptionFieldId("message") Which field to use as the entry's description
.singularApiName("Product") Singular name for GraphQL queries (e.g., getProduct)
.pluralApiName("Products") Plural name for GraphQL queries (e.g., listProducts)
.singleEntry() Makes the model a singleton (only one entry can exist). Automatically adds
常见问题

模型如何注册?

在 webiny 配置文件中作为 API 扩展注册,使用包含 .ts 扩展名的完整文件路径。

字段可以嵌套吗?

可以。该技能涵盖 object 和 dynamic zone 字段内的嵌套布局。

它会讲解如何选择字段的显示类型吗?

会。它说明了每种字段类型应使用哪种管理后台显示方式。

详情

作者的原始描述

Creating Headless CMS content models via code using the ModelFactory pattern. Use this skill when the developer wants to create, modify, or understand content model definitions, define fields and validators, set up reference fields between models, configure field layouts (including nested layouts inside object or dynamicZone fields), pick the correct Admin UI renderer for a field type (textInput/textInputs, lexicalEditor/lexicalEditors, file/files, objectAccordionSingle/objectAccordionMultiple, etc.), or work with the ModelFactory builder API. Also covers field types (text, longText, number, b

repo: webiny/webiny-js

热度

GitHub 星标
8.1k
排名
#12598
许可证
—
更新时间
2026/11/2

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能