我有一个关于Wagtail CMS的问题。
最近,我尝试以编程方式导入Wagtail Page模型实例的StreamField中的某些文档。我做了一些研究,但没有结果。
目前我正在使用:
这里是我需要将文档作为附件导入的页面模型(参见同音字段):
class EventPage(TranslatablePage, Page):
# Database fields
uuid = models.UUIDField(verbose_name='UUID', default=uuid.uuid4)
start_date = models.DateField(verbose_name='Start date')
end_date = models.DateField(verbose_name='End date')
location = models.CharField(verbose_name='Place', max_length=255, null=True, blank=True)
body = RichTextField(verbose_name='Body')
attachments = StreamField(blocks.StreamBlock([
('document', DocumentChooserBlock(label='Document', icon='doc-full-inverse')),
]), verbose_name='Attachments', null=True, blank=True)
subscribe = models.BooleanField(verbose_name='Subscribe option', default=False)
# Editor panels configuration
content_panels = [
FieldPanel('title', classname='title'),
MultiFieldPanel([
FieldRowPanel([
FieldPanel('start_date'),
FieldPanel('end_date'),
]),
], heading='Period'),
FieldPanel('location'),
FieldPanel('body'),
StreamFieldPanel('attachments'),
]
promote_panels = Page.promote_panels + [
MultiFieldPanel([
FieldPanel('subscribe'),
], heading='Custom Settings'),
]
settings_panels = TranslatablePage.settings_panels + [
MultiFieldPanel([
FieldPanel('uuid'),
], heading='Meta')
]
parent_page_types = ["home.FolderPage"]
subpage_types = []
在shell上,我尝试应用this page上解释的解决方案,但没有成功。
event = EventPage.objects.get(pk=20)
doc = Document.objects.get(pk=3)
event.attachments = [
('document',
[
StreamValue.StreamChild(
id = None,
block = DocumentChooserBlock(),
value = doc
)
]
)
]
Python给我这个错误:AttributeError:' list'对象没有属性' pk'。
答案 0 :(得分:3)
event.attachments = [('document', doc)]
应该有用。 (在the other question you link to上,StreamChild是必要的,因为AccordionRepeaterBlock是一个嵌套在StreamBlock中的StreamBlock;对于你的定义来说并非如此。)
要将文档添加到现有StreamField内容,请构建新列表并将其分配给event.attachments
:
new_attachments = [(block.block_type, block.value) for block in blocks]
new_attachments.append(('document', doc))
event.attachments = new_attachments
(目前您无法直接追加到StreamField值,但是this may well be supported in a future Wagtail release ...)