首页 > 解决方案 > Wagtail(Django CMS)如何在wagtail中添加单个图像,简单的尝试以“OperationalError no such column:gallery_gallerysubpage.cover_id”结尾

问题描述

我的问题很简单,我只是不知道如何将单个图像添加为页面缩影。这是我的页面模型(我尝试了ImageChooserPanel 参考中所示):

class GallerySubpage(Page):
    intro = models.CharField(max_length=250)
    body = RichTextField(blank=True)

    # THIS IS TAKEN FROM DOCS
    cover = models.ForeignKey(
        'wagtailimages.Image',
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name='+'
    )

    # This returns some dummy for a while. I want to replace this with some field that contain single Image
    def main_image(self):
        gallery_item = self.gallery_images.first()
        if gallery_item:
            return gallery_item.image
        else:
            return None

    search_fields = Page.search_fields + [
        index.SearchField('intro'),
        index.SearchField('body'),
    ]

    content_panels = Page.content_panels + [
        FieldPanel('intro'),
        FieldPanel('body', classname="full"),
        InlinePanel('gallery_images', label = "Images that will be displayed on this page"),

        # THIS IS TAKEN FROM DOCS
        ImageChooserPanel('cover'),

    ]

当我运行代码时出现错误:

OperationalError at /gallery/galerry132/
no such column: gallery_gallerysubpage.cover_id

标签: djangoimagemodelwagtail

解决方案


如果您没有运行./manage.py makemigrations并且./manage.py migrate在添加cover字段之后会发生上述错误。


推荐阅读