Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of StreamField in Snippets on Wagtail

I am trying to build some structured snippets on my Wagtail site. I looked through the documentation, but saw nothing on this point (forgive me if I missed it).

Is it possible to use StreamField inside of a Snippet? Or, only on Pages

like image 410
Adam Hopkins Avatar asked May 05 '16 12:05

Adam Hopkins


People also ask

What is wagtail Streamfield?

Wagtail streamfield is a sequence of blocks of differint types to be rearranged as desired.

What is Streamfield?

A streamfield, at its core, is a model for editing content. Put another way. It provides a framework for editing content, like how a rich-text editor provides you a model or framework to edit content. That's what streamfields are all about—editing content.

What is a snippet in wagtail?

Snippets are pieces of content which do not necessitate a full webpage to render. They could be used for making secondary content, such as headers, footers, and sidebars, editable in the Wagtail admin. Snippets are Django models which do not inherit the Page class and are thus not organized into the Wagtail tree.


1 Answers

Yes, you can definitely add a Streamfield to a snippet. It works the same as if you use it on a Wagtail Page subclass. Here's an example of it's use:

from wagtail.core.fields import StreamField
from wagtail.wagtailsnippets.models import register_snippet
from wagtail.wagtailadmin.edit_handlers import StreamFieldPanel

@register_snippet
class Contact(models.Model):
    contact_info = StreamField([
        ('email', MyBlocks.ContactEmail()),
        ('phone', MyBlocks.ContactPhone()),
        ('address', MyBlocks.ContactAddress()),
    ])

    panels = [StreamFieldPanel('contact_info')]

Extra stuff you didn't ask for: Streamfield is a Django model field, so it works the same on any model you define it on. Actually, the Streamfield just saves as a JSON string. The only thing that makes it different are the blocks. Blocks that are defined in that first parameter of the Streamfield are really just defining the available options that the Streamfield can use to generate content. The blocks themselves have no bearing on the SQL for CRUD operations, they're only used for manipulating the data saved for the Streamfield.

Hope that helps.

like image 98
404 Avatar answered Sep 18 '22 00:09

404