Mik*_*son 2 python django wagtail
我有一个抽象类,其中有 ha StreamField 。我还有一个继承自 BasePage 的类 CustomPage。我希望 CustomPage 向内容添加新的 StructBlock。我怎么做?
class BasePage(Page):
content = StreamField([
('ad', ...),
('text', ...),
('img', ...),
])
content_panels = Page.content_panels + [
StreamFieldPanel('content'),
]
class Meta:
abstract = True
class CustomPage(BasePage):
# add ('custom_block', ...) to content streamfield.
Run Code Online (Sandbox Code Playgroud)
StreamField 定义不能直接以这种方式“扩展”,但通过一些重新洗牌,您可以定义一个重新使用相同块列表的新 StreamField:
COMMON_BLOCKS = [
('ad', ...),
('text', ...),
('img', ...),
]
class BasePage(Page):
content = StreamField(COMMON_BLOCKS)
...
class CustomPage(BasePage):
content = StreamField(COMMON_BLOCKS + [
('custom_block', ...),
])
Run Code Online (Sandbox Code Playgroud)
或者在 StreamBlock 上使用继承(您可能认为这比连接列表更简洁:
class CommonStreamBlock(StreamBlock):
ad = ...
text = ...
img = ...
class CustomStreamBlock(CommonStreamBlock):
custom_block = ...
class BasePage(Page):
content = StreamField(CommonStreamBlock())
...
class CustomPage(BasePage):
content = StreamField(CustomStreamBlock())
Run Code Online (Sandbox Code Playgroud)
另请注意,这仅在 Django 1.10 后才有可能- 旧版本的 Django 不允许覆盖抽象超类的字段。
| 归档时间: |
|
| 查看次数: |
972 次 |
| 最近记录: |