0

I have a Page model with a StreamField:

from wagtail.core.fields import StreamField
from wagtail.embeds.blocks import EmbedBlock

class SomeModel(Page):
    stream_field = StreamField(
        [
            ("embed", EmbedBlock()),
        ]
    )

I would like to create a new SomeModel instance via code and populate the stream_field with an embed value. E.g.

from wagtail.embeds.blocks import EmbedValue

some_model_instance = SomeModel()

embed_url = "https://youtube.com/something"

embed_value = EmbedValue(embed_url)

embed_block = ("embed", embed_value)

some_model_instance.stream_field.append(embed_block)

some_model_instance.save()

The above code pattern works without error, but when I view the content in the Wagtail Admin UI the field is empty (as if nothing was provided).

How can I add content to a Wagtail StreamField EmbedBlock via code?

Brylie Christopher Oxley
  • 1,684
  • 1
  • 17
  • 34

1 Answers1

0

The way I was able to add an Embed to a StreamField is as follows:

from wagtail.embeds.embeds import get_embed

some_model_instance = SomeModel()

embed_url = "https://youtube.com/something"

embed = get_embed(embed_url)

embed_block = ("embed", embed)

some_model_instance.stream_field.append(embed_block)

some_model_instance.save()
Brylie Christopher Oxley
  • 1,684
  • 1
  • 17
  • 34