Browse Source

Add documentation for using a StructBlock inside of a StreamField

Fixes #10231
Ramon Wenger 2 years ago
parent
commit
2ca09bdc37
4 changed files with 26 additions and 0 deletions
  1. 1 0
      CHANGELOG.txt
  2. 1 0
      CONTRIBUTORS.rst
  3. 1 0
      docs/releases/5.1.md
  4. 23 0
      docs/topics/streamfield.md

+ 1 - 0
CHANGELOG.txt

@@ -6,6 +6,7 @@ Changelog
 
  * Fix: Prevent choosers from failing when initial value is an unrecognised ID, e.g. when moving a page from a location where `parent_page_types` would disallow it (Dan Braghis)
  * Docs: Document how to add non-ModelAdmin views to a `ModelAdminGroup` (Onno Timmerman)
+ * Docs: Document how to add StructBlock data to a StreamField (Ramon Wenger)
  * Maintenance: Switch to ruff for flake8 / isort code checking (Oliver Parker)
 
 

+ 1 - 0
CONTRIBUTORS.rst

@@ -708,6 +708,7 @@ Contributors
 * Steve Steinwand
 * Swojak-A
 * fidoriel
+* Ramon Wenger
 
 Translators
 ===========

+ 1 - 0
docs/releases/5.1.md

@@ -24,6 +24,7 @@ depth: 1
 ### Documentation
 
  * Document how to add non-ModelAdmin views to a `ModelAdminGroup` (Onno Timmerman)
+ * Document how to add StructBlock data to a StreamField (Ramon Wenger)
 
 ### Maintenance
 

+ 23 - 0
docs/topics/streamfield.md

@@ -519,6 +519,29 @@ my_page.body.append(('paragraph', RichText("<p>And they all lived happily ever a
 my_page.save()
 ```
 
+If a block extending a StructBlock is to be used inside of the StreamField's value, the value of this block can be provided as a python dict (similar in what is accepted by the block's `.to_python` method).
+
+```python
+
+from wagtail import blocks
+
+class UrlWithTextBlock(blocks.StructBlock):
+   url = blocks.URLBlock()
+   text = blocks.TextBlock()
+
+# using this block inside the content
+
+data = {
+    'url': 'https://github.com/wagtail/',
+    'text': 'A very interesting and useful repo'
+}
+
+# append the new block to the stream as a tuple with the defined index for this block type
+my_page.body.append(('url', data))
+my_page.save()
+
+```
+
 (streamfield_retrieving_blocks_by_name)=
 
 ## Retrieving blocks by name