DocumentChooserBlocks必须有外键吗?

时间:2016-10-15 00:41:24

标签: django-models foreign-key-relationship wagtail wagtail-streamfield

我正在使用Wagtail流域来允许用户在编辑器界面中上传和链接到文档。最初,我尝试使用文档中引用的外键以及我看到的所有其他示例。我在运行迁移时遇到错误,但是文件没有属性“set name”。所以我决定不使用外键,因为这些文件不一定需要与我们的目的的一对多关系相关。所以在我的模型中,我没有使用DocumentChooserBlocks的所有字段使用外键,一切似乎工作正常。我误解了“外键”并犯了一个错误(或者练习糟糕的数据库设计)。这是我的工作模式:

class AgendaPage(Page):
author= models.CharField(max_length=255)
date = models.DateField('Post date')
mtg_date = models.DateField(default=datetime.date.today)
mtg_time = models.CharField(max_length=255, default ='10:00 AM')
full_video_url-models.CharField(required =False)
###full_audio = DocumentChooserBlock(required=False)
###mtg_transcript = DocumentChooserBlock(required=False)
])
agenda = StreamField([
    ('agenda_item', blocks.StreamBlock([
        ('item_title', blocks.TextBlock()),
        ('item_text', blocks.TextBlock()),
        ('mtg_doc', blocks.StructBlock([
            ('mtg_doc_upload', DocumentChooserBlock(required=True)),
            ('submitted_late', blocks.BooleanBlock(required=False, help_text='Submitted Late')),
            ('heldover', blocks.BooleanBlock(required=False, help_text='Held Over')),
            ('heldover_from', blocks.DateBlock(required=False, help_text="Held Over From")),
        ])),
        ('item_audio', DocumentChooserBlock(required=False)),
    ]))
])




content_panels = Page.content_panels + [
    FieldPanel('author'),
    FieldPanel('date'),
    FieldPanel('mtg_date'),
    FieldPanel('mtg_time'),
    StreamFieldPanel('agenda'),

]

此外,在模型中的两个注释掉的行中,我正在尝试使用不在流域内的DocumentChooserBlock(没有外键)我知道这种语法可能是错误的,因为我看到的所有示例都定义了forein键在模型定义中,然后在面板定义中引用DocumentChooser Panel。没有外键可以(或建议)这样做吗?

1 个答案:

答案 0 :(得分:1)

DocumentChooserBlock永远不会与外键一起使用。将文档附加到页面有两种不同的方法,您可以选择其中一种:

  • ForeignKey到文档,DocumentChooserPanel位于content_panels。当您从页面到文档具有一对一或多对一关系时,将使用此方法;例如,ProductPage,其中产品具有PDF数据表。这将在数​​据库级别创建两个对象之间的正式链接。

  • 其中包含StreamField的{​​{1}}。这用于更灵活的安排,其中文档链接可以出现在页面的任何位置。在数据库级别没有正式的关联 - 从数据库的角度来看,StreamField只是自由文本。这意味着不需要ForeignKey。