我的 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
- 但这只会发生在非core
模型上的(流)字段中。
传递请求的默认值 - 无论是它"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,所以我在使用 Python 3 时仅限于 Django 1.8。很高兴使用 wagtail 1.5。