在一个继承的类中扩展wagtail Streamfields [英] Extending wagtail Streamfields in an inherited class

查看:200
本文介绍了在一个继承的类中扩展wagtail Streamfields的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个抽象类,其中有一个StreamField。我还有一个CustomPage类继承自BasePage。我希望CustomPage向内容添加一个新的StructBlock。我如何做?

  class BasePage(页):
content = StreamField([
'ad',...),
('text',...),
('img',...),
])
content_panels = Page .content_panels + [
StreamFieldPanel('content')
]

class Meta:
abstract = True

class CustomPage(BasePage ):
#add('custom_block',...)到内容流域。


解决方案

StreamField定义不能直接扩展以这种方式,但是有一点重新洗牌,您可以定义一个重新使用相同阻止列表的新StreamField:

  COMMON_BLOCKS = [
('ad',...),
('text',...),
('img',...),
]

class BasePage(页):
content = StreamField(COMMON_BLOCKS)
...

class CustomPage(BasePage):
content = StreamField(COMMON_BLOCKS + [
('custom_block',...),
])

或者在StreamBlock上使用继承(你可能认为它比连接列表有点整洁:

  class CommonStreamBlock StreamBlock)
ad = ...
text = ...
img = ...

class CustomStreamBlock(CommonStreamBlock):
custom_block = ...

class BasePage(页):
content = St reamField(CommonStreamBlock())
...

class CustomPage(BasePage):
content = StreamField(CustomStreamBlock())

另外请注意,这是只有在Django 1.10之后才可能 - 旧版本的Django不允许覆盖抽象超类的字段。


I have a abstract class that have ha StreamField in it. I also have a class CustomPage that inherits from BasePage. I want CustomPage to add a new StructBlock to content. How do i do that?

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.

解决方案

The StreamField definition can't be 'extended' directly in this way, but with a bit of re-shuffling you can define a new StreamField that re-uses the same block list:

COMMON_BLOCKS = [
    ('ad', ...),
    ('text', ...),
    ('img', ...),
]

class BasePage(Page):
    content = StreamField(COMMON_BLOCKS)
    ...

class CustomPage(BasePage):
    content = StreamField(COMMON_BLOCKS + [
        ('custom_block', ...),
    ])

Or using inheritance on StreamBlock (which you might consider a bit neater than concatenating lists:

class CommonStreamBlock(StreamBlock):
    ad = ...
    text = ...
    img = ...

class CustomStreamBlock(CommonStreamBlock):
    custom_block = ...

class BasePage(Page):
    content = StreamField(CommonStreamBlock())
    ...

class CustomPage(BasePage):
    content = StreamField(CustomStreamBlock())

Also, note that this is only possible since Django 1.10 - older versions of Django don't allow overriding fields of an abstract superclass.

这篇关于在一个继承的类中扩展wagtail Streamfields的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆