在多个应用程序中使用流场:迁移错误
Use streamfield in multiple apps: migration error
我的 wagtail 网站项目分为不同的应用程序,如 core
、event
、publications
等,我在 core.models
中设置了流场。
现在我想不仅在 core.models
中而且在 event.models
中重用这个流场。
但是,仅定义一次我的流场(子类化 StreamBlock
)并在我的所有应用程序中重用它的最优雅(干)方式是什么?
我的 StreamBlock
的灵感来自 wagtaildemo 项目:
# core/models.py
class StoryBlock(StreamBlock):
h2 = CharBlock(icon="title", classname="title")
h3 = CharBlock(icon="title", classname="title")
h4 = CharBlock(icon="title", classname="title")
intro = RichTextBlock(icon="pilcrow")
paragraph = RichTextBlock(icon="pilcrow")
aligned_image = ImageBlock(label="Aligned image")
pullquote = PullQuoteBlock()
read_on = ReadOnBlock()
-
# event/models.py
from wagtail.wagtailcore.fields import StreamField
from wagtail.wagtailadmin.edit_handlers import FieldPanel, StreamFieldPanel
from core.models import StoryBlock
class EventIndexPage(Page):
body = StreamField(StoryBlock())
content_panels = Page.content_panels + [
StreamFieldPanel('body'),
]
但是尝试 makemigrations
更新后的 EventIndexPage
页面模型会发出警告,提示我是 trying to add a non-nullable field 'body' to eventindexpage without a default
- 但这只发生在非 [=13] 上的(流)字段=]-模型。
传递请求的默认值 - "asdf"
或 "[]"
- 到 makemigrations
构建迁移文件,但以下 migrate
失败:
$ python manage.py migrate
...
File "/myproject/venv/lib/python3.5/site-packages/wagtail/wagtailcore/fields.py", line 90, in get_prep_value
return json.dumps(self.stream_block.get_prep_value(value), cls=DjangoJSONEncoder)
File "/myproject/venv/lib/python3.5/site-packages/wagtail/wagtailcore/blocks/stream_block.py", line 205, in get_prep_value
for child in value # child is a BoundBlock instance
File "/myproject/venv/lib/python3.5/site-packages/wagtail/wagtailcore/blocks/stream_block.py", line 205, in <listcomp>
for child in value # child is a BoundBlock instance
AttributeError: 'str' object has no attribute 'block'
如果这与我的问题有关:这个项目将部署到 openshift,所以我在使用时仅限于 Django 1.8 Python 3. 很高兴使用 wagtail 1.5。
与 gasman pointeted out in in his 一样,使用 ""
作为迁移的默认值就成功了。
我的 wagtail 网站项目分为不同的应用程序,如 core
、event
、publications
等,我在 core.models
中设置了流场。
现在我想不仅在 core.models
中而且在 event.models
中重用这个流场。
但是,仅定义一次我的流场(子类化 StreamBlock
)并在我的所有应用程序中重用它的最优雅(干)方式是什么?
我的 StreamBlock
的灵感来自 wagtaildemo 项目:
# core/models.py
class StoryBlock(StreamBlock):
h2 = CharBlock(icon="title", classname="title")
h3 = CharBlock(icon="title", classname="title")
h4 = CharBlock(icon="title", classname="title")
intro = RichTextBlock(icon="pilcrow")
paragraph = RichTextBlock(icon="pilcrow")
aligned_image = ImageBlock(label="Aligned image")
pullquote = PullQuoteBlock()
read_on = ReadOnBlock()
-
# event/models.py
from wagtail.wagtailcore.fields import StreamField
from wagtail.wagtailadmin.edit_handlers import FieldPanel, StreamFieldPanel
from core.models import StoryBlock
class EventIndexPage(Page):
body = StreamField(StoryBlock())
content_panels = Page.content_panels + [
StreamFieldPanel('body'),
]
但是尝试 makemigrations
更新后的 EventIndexPage
页面模型会发出警告,提示我是 trying to add a non-nullable field 'body' to eventindexpage without a default
- 但这只发生在非 [=13] 上的(流)字段=]-模型。
传递请求的默认值 - "asdf"
或 "[]"
- 到 makemigrations
构建迁移文件,但以下 migrate
失败:
$ python manage.py migrate
...
File "/myproject/venv/lib/python3.5/site-packages/wagtail/wagtailcore/fields.py", line 90, in get_prep_value
return json.dumps(self.stream_block.get_prep_value(value), cls=DjangoJSONEncoder)
File "/myproject/venv/lib/python3.5/site-packages/wagtail/wagtailcore/blocks/stream_block.py", line 205, in get_prep_value
for child in value # child is a BoundBlock instance
File "/myproject/venv/lib/python3.5/site-packages/wagtail/wagtailcore/blocks/stream_block.py", line 205, in <listcomp>
for child in value # child is a BoundBlock instance
AttributeError: 'str' object has no attribute 'block'
如果这与我的问题有关:这个项目将部署到 openshift,所以我在使用时仅限于 Django 1.8 Python 3. 很高兴使用 wagtail 1.5。
与 gasman pointeted out in in his ""
作为迁移的默认值就成功了。