Add documentation for using a StructBlock inside of a StreamField

Fixes #10231
pull/10192/head
Ramon Wenger 2023-03-13 15:43:01 +01:00 zatwierdzone przez Matt Westcott
rodzic 23a8fe8e0e
commit 2ca09bdc37
4 zmienionych plików z 26 dodań i 0 usunięć

Wyświetl plik

@ -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)

Wyświetl plik

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

Wyświetl plik

@ -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

Wyświetl plik

@ -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