WagtailのPage Modelを作る
Wagtail Tutorials #2: Create Page Model を進めていく中で、Wgatialプロジェクトの中にblogアプリを作成し、Page Modelを作るという流れがあった。その中で躓いたポイントについてまとめる。
開発環境
- macOS Mojave ver10.14.5
- nvim
- tmux
- Django 2.2.2
- Wagtail
やったこと
Wagtail Tutorials #1: Create Wagtail Project を完了
$ cd wagtail/wagtail_tuto
$ python manage.py startapp blog
$ nvim wagtail_tuto/settings/base.py
// add 'blog' to 'INSTALLED_APPS'
その後blog/models.pyを編集する。
Tutorial通りには以下のように書かれてある。
blog/models.py
from django.db import models
class BlogPage(Page):
description = models.CharField(max_length=255, blank=True,)
content_panels = Page.content_panels + [
FieldPanel('description', classname="full")
]
class PostPage(Page):
body = RichTextField(blank=True)
content_panels = Page.content_panels + [
FieldPanel('body', classname="full"),
]
問題
その後migrationを行おうとするが、以下のようになった。
アプリ名を指定するも結果は同じ。
$ python manage.py makemigrations
No changes detected
$ python manage.py makemigrations blog
No installed app with label 'blog'.
解決策
blog/models.pyを以下のように編集。
wagtailで必要なモジュールをそれぞれインポートする必要があった。
blog/models.py
from django.db import models
from wagtail.core.models import Page
from wagtail.core.fields import RichTextField
from wagtail.admin.edit_handlers import FieldPanel
class BlogPage(Page):
description = models.CharField(max_length=255, blank=True,)
content_panels = Page.content_panels + [
FieldPanel('description', classname="full")
]
class PostPage(Page):
body = RichTextField(blank=True)
content_panels = Page.content_panels + [
FieldPanel('body', classname="full"),
]
そしてmigrationを行う。
$ python manage.py makemigrations
~ModuleNotFoundError: No module named 'wagtail.wagtailadmin'
というエラーが起きて再びmigrationはできなかった。
そこでwagtailのモジュールのパスをアップデートすると解決した。
$ wagtail updatemodulepaths
blog/models.py - 1 change
Checked 22 .py files, 1 file updated.
$ python manage.py makemigrations
Migrations for 'blog':
blog/migrations/0001_initial.py
- Create model BlogPage
- Create model PostPage
$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, blog, contenttypes, home, sessions, taggit, wagtailadmin, wagtailcore, wagtaildocs, wagtailembeds, wagtailforms, wagtailimages, wagtailredirects, wagtailsearch, wagtailusers
Running migrations:
Applying blog.0001_initial... OK
学び
- 名前がないのなら、定義していないかインポートできるかどうかを疑う。
- またパスが正しいかどうかも考慮する。