Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
313 lines (246 sloc) 8.77 KB

Django StreamField

This is a simple realisation of StreamField's idea of Wagtail CMS for plain Django admin or with Grappelli skin.


You can build your page with different kind of blocks. Sort them and sort the lists inside the blocks.

For editing content inside the blocks, it use native popup mechanism in Django admin interface. This allow you to use other field's widgets inside the blocks as is. For example, if you want to use in your blocks FileBrowseField from django-filebrowser, it will perfectly working without any additional settings.

Module also working with Grappelli Interface (Optional)

django-streamfield demo screenshot



pip install django-streamfield

How to use

1. Create new app called streamblocks

2. Put to streamblocks/ some models

...that you want to use in your stream field. And add this models in STREAMBLOCKS_MODELS list. For example:

# streamblocks/

# one object
class RichText(models.Model):
    text = models.TextField(blank=True, null=True)   

    class Meta:
        # This will use as name of block in admin

# list of objects
class ImageWithText(models.Model):
    image = models.ImageField(upload_to="folder/")
    text = models.TextField(null=True, blank=True)
    # StreamField option for list of objects
    as_list = True

    class Meta:
        verbose_name="Images with text"

# Register blocks for StreamField as list of models

3. Create templates for each models above, named as lowercase names of the models:

  1. streamblocks/templates/streamblocks/richtext.html
  2. streamblocks/templates/streamblocks/imagewithtext.html

And use block_content as context.

Note: block_content will be single object if no 'as_list' property in your model, and will be a list of objects if there is.

<div class="rich-text-block">
    {{ block_content.text|safe }}
<ul class="image-with-text-block">
    {% for block in block_content %}
        <img src="{{ block.image.url }}" alt="">
        <p>{{ block.text }}</p>
    {% endfor %}

4. Add apps to



5. Add streamfield.urls to main

urlpatterns += [
    path('streamfield/', include('streamfield.urls'))

6. Add StreamField to your model in your application

And add the models that you want to use in this stream as model_list

from streamfield.fields import StreamField
from streamblocks.models import RichText, ImageWithText

class Page(models.Model):
    stream = StreamField(
        verbose_name="Page blocks"

Then if you have your 'page' in context, you can get content by field cached property

<div class="content">
    {{ }}


Custom admin class for block's models

Models will automaticaly register in admin. If you want provide custom admin class, first unregister models and register again, using StreamBlocksAdmin class.

# streamblocks/

from django.contrib import admin
from streamfield.admin import StreamBlocksAdmin

from streamblocks.models import RichText
class RichTextBlockAdmin(StreamBlocksAdmin, admin.ModelAdmin):

Custom templates for render block models in admin

If you need to customize admin templates for block models wich you are using, you need to put templates named as described in section 3 (above). but put it inside "admin" folder.

For example for RichText block it will be:


As context use "form":

{{ form.text.value }}

Override how to render block's fields in admin

Create custom template for field with name as lowercased field widget name, and put it inside .../streamblocks/admin/fields/ folder.

For example for TextField widget (Textarea) of RichText block, it will be:


As context use "field":

{{ field.value|default:""|safe }}

Override list of blocks for your StreamField in

Typicaly you set the blocks in your models as model_list attribute of StreamField field. But if you want to change blocks, for example depending on object, you can do it in admin site of your model. Suppose you want to use only RichText on page with id=1.

from streamfield.fields import StreamFieldWidget
from streamblocks.models import RichText
from .models import Page

class PageAdmin(models.Admin):

        form = super().get_form(request, obj, **kwargs)
        if obj and == 1:
            form.base_fields['stream'].widget = StreamFieldWidget(attrs={
                'model_list': [ RichText ]
        return form

Be careful with already existing blocks in db. If you remove them from admin, it produce error.

Block options

You may use options property in your streamblocks models to add some additional options to your block. This is useful with as_list property when you need to add some options to whole block not separatly to each object of this list.

For example:

# streamblocks/

# list of objects as slider
class Slide(models.Model):
    image = models.ImageField(upload_to="folder/")
    text = models.TextField(null=True, blank=True)
    # StreamField option for list of objects
    as_list = True
    options = {
        "autoplay": {
            "label": "Autoplay slider",
            "type": "checkbox",
            "default": False

    class Meta:

In block template you can use this options as options.autoplay In page admin you will see it on the bottom of this block.

Note: Now only "checkbox" type is working. You may apply options for all blocks with STREAMFIELD_BLOCK_OPTIONS (See Settings)

Special cases

Complex Blocks

You may use StreamField as part of blocks and create with that way complex structure and use {{ block_content.<field_name>.render }}

Blocks without data in database. Only templates.

You may use it for widgets or separators or for whatever you want... Just make the block model abstract.

class EmptyBlock(models.Model):
    class Meta:
        abstract = True
        verbose_name='Empty space'

and use streamblocks/templates/streamblocks/emptyblock.html for your content.

Note: Don't forget to register a block in STREAMBLOCKS_MODELS

Cache for reduce the number of database requests

There is two ways of caching:

  • Simple cache view with django cache
  • Create additional field, for example: 'stream_rendered' and render to this field html in save method
    self.stream_rendered =

...and use this field in your html




If your app streamblocks located not in project root directory, you need to reflect it in



You may use STREAMFIELD_BLOCK_OPTIONS in to add some options to all blocks.

For example:

    "margins": {
        "label": "Margins",
        "type": "checkbox",
        "default": True

In block template use {{ options.margins }}

Note: Now only "checkbox" type is working.

You can’t perform that action at this time.