2017-01-08 4 views
1

私にはStreamFieldがある抽象クラスがあります。私はまた、BasePageから継承するクラスCustomPageを持っています。 CustomPageにコンテンツに新しいStructBlockを追加したい。それ、どうやったら出来るの?継承されたクラスのwagtail Streamfieldsを拡張する

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. 

答えて

2

StreamField定義は、このように直接「拡張」することはできませんが、再シャッフルのビットとあなたが同じブロックリストを再使用する新しいStreamField定義することができます。

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

class BasePage(Page): 
    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(Page): 
    content = StreamField(CommonStreamBlock()) 
    ... 

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

、これはonly possible since Django 1.10であることに注意してください - ジャンゴの古いバージョンにはありません抽象スーパークラスのフィールドをオーバーライドできます。

0

@gasmanソリューションの横にもう1つのソリューションが見つかりました。

Streamフィールドのdeconstructメソッドを使用して、BasePage StreamFieldからすべてのブロックを取得します。 CustomPageでコンテンツStreamFieldを作成するときに、これらのブロックを使用します。

私は今のところこれを使用しますが、私は@gasmanの最後の解決策が最も美しい解決策だと思います。

class BasePage(Page): 
    content = StreamField([ 
     ('ad', ...), 
     ('text', ...), 
     ('img', ...), 
    ]) 
    content_panels = Page.content_panels + [ 
     StreamFieldPanel('content'), 
    ] 
    @staticmethod 
    def get_content_blocks(): 
     return list(BasePage.content.field.deconstruct()[2][0]) 

    class Meta: 
     abstract = True 

class CustomPage(BasePage): 
    content = StreamField(BasePage.get_content_blocks() + 
     [ 
      ('custom_block', ....), 
     ] 
    ) 
関連する問題