创建虚拟内容

Create dummy content

我使用免费的 Contentful 帐户创建了一个新 Space 和一个名为 Post 的内容模型。每个Post字段可以有多种数据类型

我用的是

现在,在创建内容时,我可以通过在以下表格中填写信息并单击绿色按钮“发布”来手动添加 Posts。

虽然如果我创建一个 post 没问题,但如果我想创建 50 个 post 会花费太长时间(甚至复制 Post 因为它们成为草稿,仍然需要复制、稍作编辑和发布)。如何实现自动化?

此处内容丰富的 DevRel。

为了创建大量条目和新数据,我们提供 the Content Management API (CMA)。 API 的目的是在您的 Contentful space.

中执行 WRITE 操作

一种创建数百个新条目的方法,您可以使用此 WRITE API 并编写自定义脚本来创建所有条目。

例如in Node.js that could look like follows:

// Call this in a loop 
client.getSpace('<space_id>')
.then((space) => space.createEntryWithId('<content_type_id>', '<entry_id>', {
  fields: {
    title: {
      'en-US': 'Entry title'
    }
  }
}))
.then((entry) => console.log(entry))
.catch(console.error)

如果您不想自己定义 ID,可以使用 createEntry。 (我只是觉得文档缺少那个,会修复它)。


解决此问题的另一种方法是不使用“香草 CMA”。我们提供工具,例如import/export 您在 Contentful space 中拥有的所有数据。生态系统的这些工具位于 CMA 之上,并将 API 调用抽象化。

import/export tooling 可以用作提供的 npm 包或 CLI 工具。如果您不想用您选择的编程语言编写脚本,此工具会很有用。

您可以做的是 export a space to a JSON file and then adjust the JSON file with the entries that you want to create. You could then use the import command 根据 JSON 文件创建大量条目。

contentful space export
# adjust the content file
contentful space import --content-file <file>

希望对您有所帮助,让我知道进展如何。 :)