Add StreamField as a model field type, and implement 'deconstruct' for migration support

This commit is contained in:
Matt Westcott 2015-01-14 15:41:05 +00:00
parent 3386ee53e6
commit e1cf9f9196
2 changed files with 18 additions and 0 deletions

View file

@ -6,6 +6,7 @@ from django.utils.html import format_html, format_html_join
from django.utils.safestring import mark_safe
from django.utils.text import capfirst
from django.utils.encoding import python_2_unicode_compatible
from django.utils.deconstruct import deconstructible
from django.template.loader import render_to_string
from django.forms import Media
from django.forms.utils import ErrorList
@ -35,6 +36,7 @@ def js_dict(d):
# Top-level superclasses and helper objects
# =========================================
@deconstructible
class Block(object):
creation_counter = 0
@ -206,6 +208,11 @@ class TextInputBlock(Block):
# Field block
# ===========
# FIXME: form field instances are not deconstructible for migrations. Need some other way to refer to
# them in the initialiser, in the case that FieldBlock appears inline within a StreamField definition.
# (Referring to them by class would probably work; it's unlikely that any parameter passed to them
# would affect anything you're doing in migrations)
class FieldBlock(Block):
default = None

View file

@ -36,3 +36,14 @@ class RichTextField(models.TextField):
defaults = {'widget': RichTextArea}
defaults.update(kwargs)
return super(RichTextField, self).formfield(**defaults)
class StreamField(models.TextField):
def __init__(self, block_types, **kwargs):
self.block_types = block_types
super(StreamField, self).__init__(**kwargs)
def deconstruct(self):
name, path, args, kwargs = super(StreamField, self).deconstruct()
kwargs['block_types'] = self.block_types
return name, path, args, kwargs