2
0

streamfield.rst 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. .. _streamfield:
  2. How to use StreamField for mixed content
  3. ========================================
  4. StreamField provides a content editing model suitable for pages that do not follow a fixed structure -- such as blog posts or news stories -- where the text may be interspersed with subheadings, images, pull quotes and video. It's also suitable for more specialised content types, such as maps and charts (or, for a programming blog, code snippets). In this model, these different content types are represented as a sequence of 'blocks', which can be repeated and arranged in any order.
  5. For further background on StreamField, and why you would use it instead of a rich text field for the article body, see the blog post `Rich text fields and faster horses <https://torchbox.com/blog/rich-text-fields-and-faster-horses/>`__.
  6. StreamField also offers a rich API to define your own block types, ranging from simple collections of sub-blocks (such as a 'person' block consisting of first name, surname and photograph) to completely custom components with their own editing interface. Within the database, the StreamField content is stored as JSON, ensuring that the full informational content of the field is preserved, rather than just an HTML representation of it.
  7. Using StreamField
  8. -----------------
  9. ``StreamField`` is a model field that can be defined within your page model like any other field:
  10. .. code-block:: python
  11. from django.db import models
  12. from wagtail.core.models import Page
  13. from wagtail.core.fields import StreamField
  14. from wagtail.core import blocks
  15. from wagtail.admin.edit_handlers import FieldPanel, StreamFieldPanel
  16. from wagtail.images.blocks import ImageChooserBlock
  17. class BlogPage(Page):
  18. author = models.CharField(max_length=255)
  19. date = models.DateField("Post date")
  20. body = StreamField([
  21. ('heading', blocks.CharBlock(form_classname="full title")),
  22. ('paragraph', blocks.RichTextBlock()),
  23. ('image', ImageChooserBlock()),
  24. ])
  25. content_panels = Page.content_panels + [
  26. FieldPanel('author'),
  27. FieldPanel('date'),
  28. StreamFieldPanel('body'),
  29. ]
  30. In this example, the body field of ``BlogPage`` is defined as a ``StreamField`` where authors can compose content from three different block types: headings, paragraphs, and images, which can be used and repeated in any order. The block types available to authors are defined as a list of ``(name, block_type)`` tuples: 'name' is used to identify the block type within templates, and should follow the standard Python conventions for variable names: lower-case and underscores, no spaces.
  31. You can find the complete list of available block types in the :ref:`StreamField block reference <streamfield_block_reference>`.
  32. .. note::
  33. StreamField is not a direct replacement for other field types such as RichTextField. If you need to migrate an existing field to StreamField, refer to :ref:`streamfield_migrating_richtext`.
  34. .. _streamfield_template_rendering:
  35. Template rendering
  36. ------------------
  37. StreamField provides an HTML representation for the stream content as a whole, as well as for each individual block. To include this HTML into your page, use the ``{% include_block %}`` tag:
  38. .. code-block:: html+django
  39. {% load wagtailcore_tags %}
  40. ...
  41. {% include_block page.body %}
  42. In the default rendering, each block of the stream is wrapped in a ``<div class="block-my_block_name">`` element (where ``my_block_name`` is the block name given in the StreamField definition). If you wish to provide your own HTML markup, you can instead iterate over the field's value, and invoke ``{% include_block %}`` on each block in turn:
  43. .. code-block:: html+django
  44. {% load wagtailcore_tags %}
  45. ...
  46. <article>
  47. {% for block in page.body %}
  48. <section>{% include_block block %}</section>
  49. {% endfor %}
  50. </article>
  51. For more control over the rendering of specific block types, each block object provides ``block_type`` and ``value`` properties:
  52. .. code-block:: html+django
  53. {% load wagtailcore_tags %}
  54. ...
  55. <article>
  56. {% for block in page.body %}
  57. {% if block.block_type == 'heading' %}
  58. <h1>{{ block.value }}</h1>
  59. {% else %}
  60. <section class="block-{{ block.block_type }}">
  61. {% include_block block %}
  62. </section>
  63. {% endif %}
  64. {% endfor %}
  65. </article>
  66. Combining blocks
  67. ----------------
  68. In addition to using the built-in block types directly within StreamField, it's possible to construct new block types by combining sub-blocks in various ways. Examples of this could include:
  69. * An "image with caption" block consisting of an image chooser and a text field
  70. * A "related links" section, where an author can provide any number of links to other pages
  71. * A slideshow block, where each slide may be an image, text or video, arranged in any order
  72. Once a new block type has been built up in this way, you can use it anywhere where a built-in block type would be used - including using it as a component for yet another block type. For example, you could define an image gallery block where each item is an "image with caption" block.
  73. StructBlock
  74. ~~~~~~~~~~~
  75. ``StructBlock`` allows you to group several 'child' blocks together to be presented as a single block. The child blocks are passed to ``StructBlock`` as a list of ``(name, block_type)`` tuples:
  76. .. code-block:: python
  77. :emphasize-lines: 2-7
  78. body = StreamField([
  79. ('person', blocks.StructBlock([
  80. ('first_name', blocks.CharBlock()),
  81. ('surname', blocks.CharBlock()),
  82. ('photo', ImageChooserBlock(required=False)),
  83. ('biography', blocks.RichTextBlock()),
  84. ])),
  85. ('heading', blocks.CharBlock(form_classname="full title")),
  86. ('paragraph', blocks.RichTextBlock()),
  87. ('image', ImageChooserBlock()),
  88. ])
  89. When reading back the content of a StreamField (such as when rendering a template), the value of a StructBlock is a dict-like object with keys corresponding to the block names given in the definition:
  90. .. code-block:: html+django
  91. <article>
  92. {% for block in page.body %}
  93. {% if block.block_type == 'person' %}
  94. <div class="person">
  95. {% image block.value.photo width-400 %}
  96. <h2>{{ block.value.first_name }} {{ block.value.surname }}</h2>
  97. {{ block.value.biography }}
  98. </div>
  99. {% else %}
  100. (rendering for other block types)
  101. {% endif %}
  102. {% endfor %}
  103. </article>
  104. Subclassing ``StructBlock``
  105. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  106. Placing a StructBlock's list of child blocks inside a ``StreamField`` definition can often be hard to read, and makes it difficult for the same block to be reused in multiple places. As an alternative, ``StructBlock`` can be subclassed, with the child blocks defined as attributes on the subclass. The 'person' block in the above example could be rewritten as:
  107. .. code-block:: python
  108. class PersonBlock(blocks.StructBlock):
  109. first_name = blocks.CharBlock()
  110. surname = blocks.CharBlock()
  111. photo = ImageChooserBlock(required=False)
  112. biography = blocks.RichTextBlock()
  113. ``PersonBlock`` can then be used in a ``StreamField`` definition in the same way as the built-in block types:
  114. .. code-block:: python
  115. body = StreamField([
  116. ('person', PersonBlock()),
  117. ('heading', blocks.CharBlock(form_classname="full title")),
  118. ('paragraph', blocks.RichTextBlock()),
  119. ('image', ImageChooserBlock()),
  120. ])
  121. Block icons
  122. ~~~~~~~~~~~
  123. In the menu that content authors use to add new blocks to a StreamField, each block type has an associated icon. For StructBlock and other structural block types, a placeholder icon is used, since the purpose of these blocks is specific to your project. To set a custom icon, pass the option ``icon`` as either a keyword argument to ``StructBlock``, or an attribute on a ``Meta`` class:
  124. .. code-block:: python
  125. :emphasize-lines: 7
  126. body = StreamField([
  127. ('person', blocks.StructBlock([
  128. ('first_name', blocks.CharBlock()),
  129. ('surname', blocks.CharBlock()),
  130. ('photo', ImageChooserBlock(required=False)),
  131. ('biography', blocks.RichTextBlock()),
  132. ], icon='user')),
  133. ('heading', blocks.CharBlock(form_classname="full title")),
  134. ('paragraph', blocks.RichTextBlock()),
  135. ('image', ImageChooserBlock()),
  136. ])
  137. .. code-block:: python
  138. :emphasize-lines: 7-8
  139. class PersonBlock(blocks.StructBlock):
  140. first_name = blocks.CharBlock()
  141. surname = blocks.CharBlock()
  142. photo = ImageChooserBlock(required=False)
  143. biography = blocks.RichTextBlock()
  144. class Meta:
  145. icon = 'user'
  146. For a list of the recognised icon identifiers, see the :ref:`styleguide`.
  147. ListBlock
  148. ~~~~~~~~~
  149. ``ListBlock`` defines a repeating block, allowing content authors to insert as many instances of a particular block type as they like. For example, a 'gallery' block consisting of multiple images can be defined as follows:
  150. .. code-block:: python
  151. :emphasize-lines: 2
  152. body = StreamField([
  153. ('gallery', blocks.ListBlock(ImageChooserBlock())),
  154. ('heading', blocks.CharBlock(form_classname="full title")),
  155. ('paragraph', blocks.RichTextBlock()),
  156. ('image', ImageChooserBlock()),
  157. ])
  158. When reading back the content of a StreamField (such as when rendering a template), the value of a ListBlock is a list of child values:
  159. .. code-block:: html+django
  160. <article>
  161. {% for block in page.body %}
  162. {% if block.block_type == 'gallery' %}
  163. <ul class="gallery">
  164. {% for img in block.value %}
  165. <li>{% image img width-400 %}</li>
  166. {% endfor %}
  167. </ul>
  168. {% else %}
  169. (rendering for other block types)
  170. {% endif %}
  171. {% endfor %}
  172. </article>
  173. StreamBlock
  174. ~~~~~~~~~~~
  175. ``StreamBlock`` defines a set of child block types that can be mixed and repeated in any sequence, via the same mechanism as StreamField itself. For example, a carousel that supports both image and video slides could be defined as follows:
  176. .. code-block:: python
  177. :emphasize-lines: 2-5
  178. body = StreamField([
  179. ('carousel', blocks.StreamBlock([
  180. ('image', ImageChooserBlock()),
  181. ('video', EmbedBlock()),
  182. ])),
  183. ('heading', blocks.CharBlock(form_classname="full title")),
  184. ('paragraph', blocks.RichTextBlock()),
  185. ('image', ImageChooserBlock()),
  186. ])
  187. ``StreamBlock`` can also be subclassed in the same way as ``StructBlock``, with the child blocks being specified as attributes on the class:
  188. .. code-block:: python
  189. class PersonBlock(blocks.StreamBlock):
  190. image = ImageChooserBlock()
  191. video = EmbedBlock()
  192. class Meta:
  193. icon = 'image'
  194. A StreamBlock subclass defined in this way can also be passed to a ``StreamField`` definition, instead of passing a list of block types. This allows setting up a common set of block types to be used on multiple page types:
  195. .. code-block:: python
  196. class CommonContentBlock(blocks.StreamBlock):
  197. heading = blocks.CharBlock(form_classname="full title")
  198. paragraph = blocks.RichTextBlock()
  199. image = ImageChooserBlock()
  200. class BlogPage(Page):
  201. body = StreamField(CommonContentBlock())
  202. When reading back the content of a StreamField, the value of a StreamBlock is a sequence of block objects with ``block_type`` and ``value`` properties, just like the top-level value of the StreamField itself.
  203. .. code-block:: html+django
  204. <article>
  205. {% for block in page.body %}
  206. {% if block.block_type == 'carousel' %}
  207. <ul class="carousel">
  208. {% for slide in block.value %}
  209. {% if slide.block_type == 'image' %}
  210. <li class="image">{% image slide.value width-200 %}</li>
  211. {% else %}
  212. <li> class="video">{% include_block slide %}</li>
  213. {% endif %}
  214. {% endfor %}
  215. </ul>
  216. {% else %}
  217. (rendering for other block types)
  218. {% endif %}
  219. {% endfor %}
  220. </article>
  221. Limiting block counts
  222. ~~~~~~~~~~~~~~~~~~~~~
  223. By default, a StreamField can contain an unlimited number of blocks. The ``min_num`` and ``max_num`` options on ``StreamField`` or ``StreamBlock`` allow you to set a minimum or maximum number of blocks:
  224. .. code-block:: python
  225. body = StreamField([
  226. ('heading', blocks.CharBlock(form_classname="full title")),
  227. ('paragraph', blocks.RichTextBlock()),
  228. ('image', ImageChooserBlock()),
  229. ], min_num=2, max_num=5)
  230. Or equivalently:
  231. .. code-block:: python
  232. class CommonContentBlock(blocks.StreamBlock):
  233. heading = blocks.CharBlock(form_classname="full title")
  234. paragraph = blocks.RichTextBlock()
  235. image = ImageChooserBlock()
  236. class Meta:
  237. min_num = 2
  238. max_num = 5
  239. The ``block_counts`` option can be used to set a minimum or maximum count for specific block types. This accepts a dict, mapping block names to a dict containing either or both of ``min_num`` and ``max_num``. For example, to permit between 1 and 3 'heading' blocks:
  240. .. code-block:: python
  241. body = StreamField([
  242. ('heading', blocks.CharBlock(form_classname="full title")),
  243. ('paragraph', blocks.RichTextBlock()),
  244. ('image', ImageChooserBlock()),
  245. ], block_counts={
  246. 'heading': {'min_num': 1, 'max_num': 3},
  247. })
  248. Or equivalently:
  249. .. code-block:: python
  250. class CommonContentBlock(blocks.StreamBlock):
  251. heading = blocks.CharBlock(form_classname="full title")
  252. paragraph = blocks.RichTextBlock()
  253. image = ImageChooserBlock()
  254. class Meta:
  255. block_counts = {
  256. 'heading': {'min_num': 1, 'max_num': 3},
  257. }
  258. .. _streamfield_per_block_templates:
  259. Per-block templates
  260. -------------------
  261. By default, each block is rendered using simple, minimal HTML markup, or no markup at all. For example, a CharBlock value is rendered as plain text, while a ListBlock outputs its child blocks in a ``<ul>`` wrapper. To override this with your own custom HTML rendering, you can pass a ``template`` argument to the block, giving the filename of a template file to be rendered. This is particularly useful for custom block types derived from StructBlock:
  262. .. code-block:: python
  263. ('person', blocks.StructBlock(
  264. [
  265. ('first_name', blocks.CharBlock()),
  266. ('surname', blocks.CharBlock()),
  267. ('photo', ImageChooserBlock(required=False)),
  268. ('biography', blocks.RichTextBlock()),
  269. ],
  270. template='myapp/blocks/person.html',
  271. icon='user'
  272. ))
  273. Or, when defined as a subclass of StructBlock:
  274. .. code-block:: python
  275. class PersonBlock(blocks.StructBlock):
  276. first_name = blocks.CharBlock()
  277. surname = blocks.CharBlock()
  278. photo = ImageChooserBlock(required=False)
  279. biography = blocks.RichTextBlock()
  280. class Meta:
  281. template = 'myapp/blocks/person.html'
  282. icon = 'user'
  283. Within the template, the block value is accessible as the variable ``value``:
  284. .. code-block:: html+django
  285. {% load wagtailimages_tags %}
  286. <div class="person">
  287. {% image value.photo width-400 %}
  288. <h2>{{ value.first_name }} {{ value.surname }}</h2>
  289. {{ value.biography }}
  290. </div>
  291. Since ``first_name``, ``surname``, ``photo`` and ``biography`` are defined as blocks in their own right, this could also be written as:
  292. .. code-block:: html+django
  293. {% load wagtailcore_tags wagtailimages_tags %}
  294. <div class="person">
  295. {% image value.photo width-400 %}
  296. <h2>{% include_block value.first_name %} {% include_block value.surname %}</h2>
  297. {% include_block value.biography %}
  298. </div>
  299. Writing ``{{ my_block }}`` is roughly equivalent to ``{% include_block my_block %}``, but the short form is more restrictive, as it does not pass variables from the calling template such as ``request`` or ``page``; for this reason, it is recommended that you only use it for simple values that do not render HTML of their own. For example, if our PersonBlock used the template:
  300. .. code-block:: html+django
  301. {% load wagtailimages_tags %}
  302. <div class="person">
  303. {% image value.photo width-400 %}
  304. <h2>{{ value.first_name }} {{ value.surname }}</h2>
  305. {% if request.user.is_authenticated %}
  306. <a href="#">Contact this person</a>
  307. {% endif %}
  308. {{ value.biography }}
  309. </div>
  310. then the ``request.user.is_authenticated`` test would not work correctly when rendering the block through a ``{{ ... }}`` tag:
  311. .. code-block:: html+django
  312. {# Incorrect: #}
  313. {% for block in page.body %}
  314. {% if block.block_type == 'person' %}
  315. <div>
  316. {{ block }}
  317. </div>
  318. {% endif %}
  319. {% endfor %}
  320. {# Correct: #}
  321. {% for block in page.body %}
  322. {% if block.block_type == 'person' %}
  323. <div>
  324. {% include_block block %}
  325. </div>
  326. {% endif %}
  327. {% endfor %}
  328. Like Django's ``{% include %}`` tag, ``{% include_block %}`` also allows passing additional variables to the included template, through the syntax ``{% include_block my_block with foo="bar" %}``:
  329. .. code-block:: html+django
  330. {# In page template: #}
  331. {% for block in page.body %}
  332. {% if block.block_type == 'person' %}
  333. {% include_block block with classname="important" %}
  334. {% endif %}
  335. {% endfor %}
  336. {# In PersonBlock template: #}
  337. <div class="{{ classname }}">
  338. ...
  339. </div>
  340. The syntax ``{% include_block my_block with foo="bar" only %}`` is also supported, to specify that no variables from the parent template other than ``foo`` will be passed to the child template.
  341. .. _streamfield_get_context:
  342. As well as passing variables from the parent template, block subclasses can pass additional template variables of their own by overriding the ``get_context`` method:
  343. .. code-block:: python
  344. import datetime
  345. class EventBlock(blocks.StructBlock):
  346. title = blocks.CharBlock()
  347. date = blocks.DateBlock()
  348. def get_context(self, value, parent_context=None):
  349. context = super().get_context(value, parent_context=parent_context)
  350. context['is_happening_today'] = (value['date'] == datetime.date.today())
  351. return context
  352. class Meta:
  353. template = 'myapp/blocks/event.html'
  354. In this example, the variable ``is_happening_today`` will be made available within the block template. The ``parent_context`` keyword argument is available when the block is rendered through an ``{% include_block %}`` tag, and is a dict of variables passed from the calling template.
  355. All block types, not just ``StructBlock``, support the ``template`` property. However, for blocks that handle basic Python data types, such as ``CharBlock`` and ``IntegerBlock``, there are some limitations on where the template will take effect. For further details, see :ref:`boundblocks_and_values`.
  356. Customisations
  357. --------------
  358. All block types implement a common API for rendering their front-end and form representations, and storing and retrieving values to and from the database. By subclassing the various block classes and overriding these methods, all kinds of customisations are possible, from modifying the layout of StructBlock form fields to implementing completely new ways of combining blocks. For further details, see :ref:`custom_streamfield_blocks`.
  359. .. _modifying_streamfield_data:
  360. Modifying StreamField data
  361. --------------------------
  362. A StreamField's value behaves as a list, and blocks can be inserted, overwritten and deleted before saving the instance back to the database. A new item can be written to the list as a tuple of *(block_type, value)* - when read back, it will be returned as a ``BoundBlock`` object.
  363. .. code-block:: python
  364. # Replace the first block with a new block of type 'heading'
  365. my_page.body[0] = ('heading', "My story")
  366. # Delete the last block
  367. del my_page.body[-1]
  368. # Append a block to the stream
  369. my_page.body.append(('paragraph', "<p>And they all lived happily ever after.</p>"))
  370. # Save the updated data back to the database
  371. my_page.save()
  372. .. _streamfield_migrating_richtext:
  373. Migrating RichTextFields to StreamField
  374. ---------------------------------------
  375. If you change an existing RichTextField to a StreamField, the database migration will complete with no errors, since both fields use a text column within the database. However, StreamField uses a JSON representation for its data, so the existing text requires an extra conversion step in order to become accessible again. For this to work, the StreamField needs to include a RichTextBlock as one of the available block types. (When updating the model, don't forget to change ``FieldPanel`` to ``StreamFieldPanel`` too.) Create the migration as normal using ``./manage.py makemigrations``, then edit it as follows (in this example, the 'body' field of the ``demo.BlogPage`` model is being converted to a StreamField with a RichTextBlock named ``rich_text``):
  376. .. code-block:: python
  377. # -*- coding: utf-8 -*-
  378. from django.db import models, migrations
  379. from wagtail.core.rich_text import RichText
  380. def convert_to_streamfield(apps, schema_editor):
  381. BlogPage = apps.get_model("demo", "BlogPage")
  382. for page in BlogPage.objects.all():
  383. if page.body.raw_text and not page.body:
  384. page.body = [('rich_text', RichText(page.body.raw_text))]
  385. page.save()
  386. def convert_to_richtext(apps, schema_editor):
  387. BlogPage = apps.get_model("demo", "BlogPage")
  388. for page in BlogPage.objects.all():
  389. if page.body.raw_text is None:
  390. raw_text = ''.join([
  391. child.value.source for child in page.body
  392. if child.block_type == 'rich_text'
  393. ])
  394. page.body = raw_text
  395. page.save()
  396. class Migration(migrations.Migration):
  397. dependencies = [
  398. # leave the dependency line from the generated migration intact!
  399. ('demo', '0001_initial'),
  400. ]
  401. operations = [
  402. # leave the generated AlterField intact!
  403. migrations.AlterField(
  404. model_name='BlogPage',
  405. name='body',
  406. field=wagtail.core.fields.StreamField([('rich_text', wagtail.core.blocks.RichTextBlock())]),
  407. ),
  408. migrations.RunPython(
  409. convert_to_streamfield,
  410. convert_to_richtext,
  411. ),
  412. ]
  413. Note that the above migration will work on published Page objects only. If you also need to migrate draft pages and page revisions, then edit the migration as in the following example instead:
  414. .. code-block:: python
  415. # -*- coding: utf-8 -*-
  416. import json
  417. from django.core.serializers.json import DjangoJSONEncoder
  418. from django.db import migrations, models
  419. from wagtail.core.rich_text import RichText
  420. def page_to_streamfield(page):
  421. changed = False
  422. if page.body.raw_text and not page.body:
  423. page.body = [('rich_text', {'rich_text': RichText(page.body.raw_text)})]
  424. changed = True
  425. return page, changed
  426. def pagerevision_to_streamfield(revision_data):
  427. changed = False
  428. body = revision_data.get('body')
  429. if body:
  430. try:
  431. json.loads(body)
  432. except ValueError:
  433. revision_data['body'] = json.dumps(
  434. [{
  435. "value": {"rich_text": body},
  436. "type": "rich_text"
  437. }],
  438. cls=DjangoJSONEncoder)
  439. changed = True
  440. else:
  441. # It's already valid JSON. Leave it.
  442. pass
  443. return revision_data, changed
  444. def page_to_richtext(page):
  445. changed = False
  446. if page.body.raw_text is None:
  447. raw_text = ''.join([
  448. child.value['rich_text'].source for child in page.body
  449. if child.block_type == 'rich_text'
  450. ])
  451. page.body = raw_text
  452. changed = True
  453. return page, changed
  454. def pagerevision_to_richtext(revision_data):
  455. changed = False
  456. body = revision_data.get('body', 'definitely non-JSON string')
  457. if body:
  458. try:
  459. body_data = json.loads(body)
  460. except ValueError:
  461. # It's not apparently a StreamField. Leave it.
  462. pass
  463. else:
  464. raw_text = ''.join([
  465. child['value']['rich_text'] for child in body_data
  466. if child['type'] == 'rich_text'
  467. ])
  468. revision_data['body'] = raw_text
  469. changed = True
  470. return revision_data, changed
  471. def convert(apps, schema_editor, page_converter, pagerevision_converter):
  472. BlogPage = apps.get_model("demo", "BlogPage")
  473. for page in BlogPage.objects.all():
  474. page, changed = page_converter(page)
  475. if changed:
  476. page.save()
  477. for revision in page.revisions.all():
  478. revision_data = json.loads(revision.content_json)
  479. revision_data, changed = pagerevision_converter(revision_data)
  480. if changed:
  481. revision.content_json = json.dumps(revision_data, cls=DjangoJSONEncoder)
  482. revision.save()
  483. def convert_to_streamfield(apps, schema_editor):
  484. return convert(apps, schema_editor, page_to_streamfield, pagerevision_to_streamfield)
  485. def convert_to_richtext(apps, schema_editor):
  486. return convert(apps, schema_editor, page_to_richtext, pagerevision_to_richtext)
  487. class Migration(migrations.Migration):
  488. dependencies = [
  489. # leave the dependency line from the generated migration intact!
  490. ('demo', '0001_initial'),
  491. ]
  492. operations = [
  493. # leave the generated AlterField intact!
  494. migrations.AlterField(
  495. model_name='BlogPage',
  496. name='body',
  497. field=wagtail.core.fields.StreamField([('rich_text', wagtail.core.blocks.RichTextBlock())]),
  498. ),
  499. migrations.RunPython(
  500. convert_to_streamfield,
  501. convert_to_richtext,
  502. ),
  503. ]