在Wagtail CMS中,如何在StreamField上添加文档?

2024-04-27 07:17:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个关于Wagtail CMS的问题。在

最近,我尝试以编程方式导入Wagtail页面模型实例的StreamField中的一些文档。我做了一些研究,但没有结果。在

目前我正在使用:

  • 摇尾1.13
  • Django 1.11.6款
  • Python 2.7

这里是我需要将文档作为附件导入的页面模型(请参见同音字字段):

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上解释的解决方案,但没有成功。在

^{pr2}$

Python给我这个错误:AttributeError:'list'对象没有属性'pk'。在


Tags: name文档模型trueverbosedateuuidmodels
1条回答
网友
1楼 · 发布于 2024-04-27 07:17:51

我相信event.attachments = [('document', doc)]应该行得通。(在the other question you link to上,StreamChild是必需的,因为concordionRepeaterBlock是一个嵌套在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…)

相关问题 更多 >