我有一个页面,其中包含一个 StreamField 正文和一个名为 HeadingBlock 的自定义块:
class HeadingBlock(blocks.StructBlock):
heading = blocks.CharBlock()
cssid = blocks.CharBlock()
...
class CustomPage(Page):
...
body = StreamField([
('heading', HeadingBlock()),
...
我需要将新的字符块添加到 HeadingBlock 的标题属性,但使用 shell:
>>> from custom.models import CustomPage
>>> c = CustomPage.objects.all()[0]
>>> c.body[0].heading.value = "hello world" ??? this does not work
任何人都可以帮忙吗?多谢!
编辑:我简化了 HeadingBlock,删除 ListBlock 并尝试:
>>> c.body[0].value
StructValue([('heading', u'hi'), ('cssid', u'man')])
>>> type(c.body[0].value)
<class 'wagtail.wagtailcore.blocks.struct_block.StructValue'>
>>> from wagtail.wagtailcore.blocks.struct_block import StructValue
>>> c.body[0].value = StructValue([('heading', u'hello'), ('cssid', u'world')])
>>> c.save()
但是当我进入管理界面时,这些字段是空的。我试过了:
>>> c.body[0].block.child_blocks
OrderedDict([('heading', <wagtail.wagtailcore.blocks.field_block.CharBlock object at 0x7f4c2aaf9790>), ('cssid', <wagtail.wagtailcore.blocks.field_block.CharBlock object at 0x7f4c2aaf9a90>)])
>>> c.body[0].block.child_blocks['heading']
<wagtail.wagtailcore.blocks.field_block.CharBlock object at 0x7f4c2aaf9790>
>>> c.body[0].block.child_blocks['heading'].value
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: 'CharBlock' object has no attribute 'value'
什么也没发生,我不认为这有那么难:-|