relation

他のコレクションへの参照用フィールドです。記事と著者、商品とカテゴリなど、コレクション間の関連付けに使用します。

基本例

{
	"name": "author",
	"type": "relation",
	"label": "著者",
	"collection": "authors"
}

プロパティ

プロパティ必須説明
namestringYesフィールドの識別子
type"relation"Yes"relation"を指定
labelstringNoCMS上での表示名
notestringNo編集者向けの補足メモ
requiredbooleanNo必須フィールドかどうか
collectionstringYes参照先のコレクション名
multiplebooleanNo複数選択を許可するか
displayFieldstringNo選択肢に表示するフィールド名

使用例

著者の参照

// postsコレクション
{
	"name": "author",
	"type": "relation",
	"label": "著者",
	"collection": "authors",
	"required": true
}
// authorsコレクション
{
	"name": "authors",
	"label": "著者",
	"path": "content/authors",
	"extension": "json",
	"fields": [
		{ "name": "name", "type": "string", "label": "名前", "required": true },
		{ "name": "bio", "type": "text", "label": "自己紹介" }
	],
	"pico": {
		"title": "name"
	}
}

カテゴリの参照

{
	"name": "category",
	"type": "relation",
	"label": "カテゴリ",
	"collection": "categories",
	"required": true
}

複数の関連記事

{
	"name": "relatedPosts",
	"type": "relation",
	"label": "関連記事",
	"collection": "posts",
	"multiple": true
}

displayFieldの指定

{
	"name": "author",
	"type": "relation",
	"label": "著者",
	"collection": "authors",
	"displayField": "name"
}

displayFieldを指定すると、選択肢にそのフィールドの値が表示されます。省略した場合は、参照先コレクションのpico.titleで指定されたフィールドが使用されます。

商品とブランド

// productsコレクション
{
  "name": "brand",
  "type": "relation",
  "label": "ブランド",
  "collection": "brands",
  "displayField": "name"
}

// brandsコレクション
{
  "name": "brands",
  "label": "ブランド",
  "path": "content/brands",
  "extension": "json",
  "fields": [
    { "name": "name", "type": "string", "label": "ブランド名", "required": true },
    { "name": "logo", "type": "image", "label": "ロゴ" }
  ],
  "pico": {
    "title": "name"
  }
}

タグの複数選択

{
	"name": "tags",
	"type": "relation",
	"label": "タグ",
	"collection": "tags",
	"multiple": true,
	"displayField": "name"
}

単一選択と複数選択の違い

単一選択(デフォルト)

{
	"name": "author",
	"type": "relation",
	"collection": "authors"
}
  • 1つの参照先のみ選択可能
  • 値は参照先のファイル名(スラッグ)が保存される

複数選択(multiple: true)

{
	"name": "relatedPosts",
	"type": "relation",
	"collection": "posts",
	"multiple": true
}
  • 複数の参照先を選択可能
  • 値は配列として保存される

保存形式

参照先のファイル名(拡張子なし)が保存されます。

単一選択:

---
author: 'john-doe'
---

複数選択:

---
relatedPosts:
  - 'getting-started'
  - 'advanced-tips'
  - 'best-practices'
---

JSON形式:

{
	"author": "john-doe",
	"tags": ["javascript", "tutorial", "beginner"]
}

注意点

  • 参照先のコレクションが存在している必要があります
  • 参照先のコンテンツが削除されても、参照は自動的には更新されません
  • displayFieldを指定しない場合は、参照先のpico.titleで指定されたフィールドが使用されます